如何在activejdbc中保存从表单提交的值?

时间:2017-03-24 11:37:49

标签: java activejdbc javalite

@Override
public Long createPost(Request request) {

    Base.open();

    ObjectMapper mapper = new ObjectMapper();

    try {
        Post newPost  = mapper.readValue(request.body(), Post.class);
//        Map values = ... initialize map
//        newPost.saveIt();

    } catch (IOException ex) {
        Logger.getLogger(PostServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
    }

    Base.close();

    return 1L;
}

从官方文档来看,这Map values = ... initialize map并不清楚。我可以做newPost.set("first_name", "Sam"),但有更好的方法,而不是像这样设置值吗?

2 个答案:

答案 0 :(得分:1)

我不熟悉Spark(我是ActiveWeb的作者),但您可以使用过滤器打开/关闭连接,而不是污染您的服务类:

http://sparkjava.com/documentation.html#filters

此外,如果您可以将请求参数转换为namespace WindowsFormsApplication4 { public partial class Form1 : Form { int b = 1; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Label label = new Label(); // used to print users label.Text = String.Format("{0}", b); label.Left = 10; label.Top = 25; this.Controls.Add(label); b = b + 1; Console.WriteLine(b); } } } ,则可以执行以下操作:

java.util.Map

这是ActiveWeb的一个例子,但也可以帮助你解决Spark: https://github.com/javalite/activeweb-simple/blob/master/src/main/java/app/controllers/BooksController.java

答案 1 :(得分:1)

如果我理解正确,您正在研究如何从POST请求中获取值,然后使用ActiveJDBC来保存这些值。我也很新,我们处于应用程序的开始阶段,但我们正在使用SparkJava和ActiveJDBC。

示例是实际代码,我没有时间简化它。但基本上我们为模型类创建了一个POJO。我们最初扩展了org.javalite.activejdbc.Model,但我们需要处理审计字段(创建,更新用户/时间)并帮助从JSON转换,因此我们使用名为CecilModel的自定义类扩展了它。但CecilModel扩展了Model类。

我们有一个接收请求的控制器。该请求以JSON的形式出现,该JSON与我们的模型类的字段名称相匹配。在我们的自定义CecilModel类中,我们将JSON映射到Map,然后我们使用Model.fromMap方法来训练字段并将其放入我们的自定义模型POJO中。我们不需要吸气剂或固定剂,为了方便起见,它更多。我们只需要我们的JSON请求与我们的模型中的名称相同。

以下是我们的代码,但也许您可以查看我们是如何做到的。

我们的桌子模型pojo。

package com.brookdale.model;

import java.sql.Timestamp;

import org.javalite.activejdbc.Model;
import org.javalite.activejdbc.annotations.BelongsTo;
import org.javalite.activejdbc.annotations.BelongsToParents;
import org.javalite.activejdbc.annotations.IdGenerator;
import org.javalite.activejdbc.annotations.IdName;
import org.javalite.activejdbc.annotations.Table;

import com.brookdale.model.activejdbc.CecilModel;

// This class handles mapping of data from the database to objects
// and back, including custom selection queries.

@Table("RECURRINGITEMSCHEDULE")
@BelongsToParents({
    @BelongsTo(foreignKeyName="itemID",parent=Item.class),
    @BelongsTo(foreignKeyName="contactAgreementID",parent=ContactAgreement.class),
    @BelongsTo(foreignKeyName="unitOfMeasureCode",parent=UnitOfMeasure.class)
})
@IdGenerator("SQ_RECURRINGITEMSCHEDULE.nextVal")
@IdName("recurringItemScheduleID")
public class RecurringItem extends CecilModel {

    public Long getRecurringItemScheduleID() {
        return getLong("recurringItemScheduleID");
    }
    public void setRecurringItemScheduleID(Long recurringItemScheduleID) {
        set("recurringItemScheduleID",recurringItemScheduleID);
    }
    public Long getContactAgreementID() {
        return getLong("contactAgreementID");
    }
    public void setContactAgreementID(Long contactAgreementID) {
        set("contactAgreementID",contactAgreementID);
    }
    public Long getItemID() {
        return getLong("itemID");
    }
    public void setItemID(Long itemID) {
        set("itemID",itemID);
    }
    public Double getUnitChargeAmt() {
        return getDouble("unitChargeAmt");
    }
    public void setUnitChargeAmt(Double unitChargeAmt) {
        set("unitChargeAmt",unitChargeAmt);
    }
    public Integer getUnitQty() {
        return getInteger("unitQty");
    }
    public void setUnitQty(Integer unitQty) {
        set("unitQty",unitQty);
    }
    public String getUnitOfMeasureCode() {
        return getString("unitOfMeasureCode");
    }
    public void setUnitOfMeasureCode(String unitOfMeasureCode) {
        set("unitOfMeasureCode",unitOfMeasureCode);
    }
    public Timestamp getLastGeneratedPeriodEndDate() {
        return getTimestamp("lastGeneratedPeriodEndDate");
    }
    public void setLastGeneratedPeriodEndDate(Timestamp lastGeneratedPeriodEndDate) {
        set("lastGeneratedPeriodEndDate",lastGeneratedPeriodEndDate);
    }
    public Timestamp getEffEndDate() {
        return getTimestamp("effEndDate");
    }
    public void setEffEndDate(Timestamp effEndDate) {
        set("effEndDate",effEndDate);
    }
    public Timestamp getEffStartDate() {
        return getTimestamp("effStartDate");
    }
    public void setEffStartDate(Timestamp effStartDate) {
        set("effStartDate",effStartDate);
    }

    @Override
    public void validate() {
        validatePresenceOf("unitofmeasurecode","itemid","unitqty","effstartdate","unitChargeAmt","contactAgreementID");
        validateNumericalityOf("itemid","unitQty","contactAgreementID");
        // check to make sure this is an update operation
        if (!this.isNew()) {
            RecurringItem ridb = RecurringItem.findById(this.getId());
            if (ridb.getLastGeneratedPeriodEndDate() != null) {
                if (this.getItemID() != ridb.getItemID())
                    this.addError("itemid", "Item can not be updated once a charge has been created.");
                if (!this.getEffStartDate().equals(ridb.getEffStartDate()))
                    this.addError("effstartdate", "Effective start date can not be updated once a charge has been created.");
                if (this.getUnitChargeAmt() != ridb.getUnitChargeAmt())
                    this.addError("unitchargeamt", "Unit charge amount can not be updated after last generated period end date has been set.");
                if (this.getUnitQty() != ridb.getUnitQty())
                    this.addError("unitqty", "Unit quantity can not be updated after last generated period end date has been set.");
                if (!this.getUnitOfMeasureCode().equals(ridb.getUnitOfMeasureCode()))
                    this.addError("", "Unit of measure can not be updated after last generated period end date has been set.");
            }
        }

        if (this.getEffEndDate() != null && this.getEffStartDate().compareTo(this.getEffEndDate()) >= 0) {
            this.addError("effenddate", "Effective end date can not come before the start date.");
        }

    }

}

扩展我们的自定义Model类。这将扩展实际的ActiveJDBC Model类。

package com.brookdale.model.activejdbc;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.javalite.activejdbc.Model;
import org.javalite.activejdbc.validation.ValidationBuilder;
import org.javalite.activejdbc.validation.ValidatorAdapter;

import com.brookdale.core.CLArgs;
import com.brookdale.security.bo.User;

public abstract class CecilModel extends Model {

    private static final transient TypeReference<HashMap<String, Object>> mapType = new TypeReference<HashMap<String, Object>>() {};
    private static final transient TypeReference<LinkedList<HashMap<String, Object>>> listMapType = new TypeReference<LinkedList<HashMap<String, Object>>>() {};
    private static final transient SimpleDateFormat jsonDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");


    public Timestamp getUpdateDateTime() {
        return getTimestamp("updateDateTime");
    }
    public void setUpdateDateTime(LocalDateTime updateDateTime) {
        set("updateDateTime",updateDateTime == null ? null : Timestamp.valueOf(updateDateTime));
    }
    public Timestamp getCreateDateTime() {
        return getTimestamp("createDateTime");
    }
    public void setCreateDateTime(LocalDateTime createDateTime) {
        set("createDateTime",createDateTime == null ? null : Timestamp.valueOf(createDateTime));
    }
    public String getUpdateUsername() {
        return getString("updateUsername");
    }
    public void setUpdateUsername(String updateUsername) {
        set("updateUsername",updateUsername);
    }
    public String getCreateUsername() {
        return getString("createUsername");
    }
    public void setCreateUsername(String createUsername) {
        set("createUsername",createUsername);
    }
    public Long getUpdateTimeId() {
        return getLong("updateTimeId");
    }
    public void setUpdateTimeId(Long updateTimeId) {
        set("updateTimeId",updateTimeId);
    }

    public boolean save(User user) {
        String userId = (CLArgs.args.isAuthenabled()) ? user.getUserid() : "TEST_MODE";
        // insert
        java.sql.Timestamp now = java.sql.Timestamp.valueOf(java.time.LocalDateTime.now());
        if (this.getId() == null || this.getId().toString().equals("0")) {
            this.setId(null);
            this.set("createDateTime", now);
            this.set("createUsername", userId);
            this.set("updateDateTime", now);
            this.set("updateUsername", userId);
            this.set("updateTimeId", 1);
        } 
        // update
        else {
            Long updatetimeid = this.getLong("updateTimeid");
            this.set("updateDateTime", now);
            this.set("updateUsername", userId);
            this.set("updateTimeId",  updatetimeid == null ? 1 : updatetimeid + 1);
        }
        return super.save();
    }

    public boolean saveIt(User user) {
        String userId = (CLArgs.args.isAuthenabled()) ? user.getUserid() : "TEST_MODE";
        // insert
        java.sql.Timestamp now = java.sql.Timestamp.valueOf(java.time.LocalDateTime.now());
        if (this.isNew()) {
            this.setId(null);
            this.set("createDateTime", now);
            this.set("createUsername", userId);
            this.set("updateDateTime", now);
            this.set("updateUsername", userId);
            this.set("updateTimeId", 1);
        } 
        // update
        else {
            Long updatetimeid = this.getLong("updateTimeid");
            this.set("updateDateTime", now);
            this.set("updateUsername", userId);
            this.set("updateTimeId",  updatetimeid == null ? 1 : updatetimeid + 1);
        }
        return super.saveIt();
    }

    public boolean saveModel(User user, boolean insert) {
        if(insert){
            this.insertIt(user);
        }else{
            this.updateIt(user);
        }
        return super.saveIt();
    }

    public boolean insertIt(User user) {
        // insert
        java.sql.Timestamp now = java.sql.Timestamp.valueOf(java.time.LocalDateTime.now());     
        this.setId(null);
        this.set("createDateTime", now);
        this.set("createUsername", user.getUserid());
        this.set("updateDateTime", now);
        this.set("updateUsername", user.getUserid());
        this.set("updateTimeId", 1);
        return super.saveIt();
    }

    public boolean updateIt(User user) {
        // insert
        java.sql.Timestamp now = java.sql.Timestamp.valueOf(java.time.LocalDateTime.now());     
        Long updatetimeid = this.getLong("updateTimeid");
        this.set("updateDateTime", now);
        this.set("updateUsername", user.getUserid());
        this.set("updateTimeId",  updatetimeid == null ? 1 : updatetimeid + 1);
        return super.saveIt();
    }

    // Convert a single ActiveJdbc Object to a json string
    @SuppressWarnings("unchecked")
    public String toJson() {
        Map<String, Object> objMap = this.toMap();
        try {
            if (objMap.containsKey("parents")) {
                Map<String, ?> parentsMap = (Map<String, ?>) objMap.get("parents");
                for (String key: parentsMap.keySet()) {
                    objMap.put(key, parentsMap.get(key));
                }
                objMap.remove("parents");
            }
            if (objMap.containsKey("children")) {
                Map<String, ?> childrenMap = (Map<String, ?>) objMap.get("children");
                for (String key: childrenMap.keySet()) {
                    objMap.put(key, childrenMap.get(key));
                }
                objMap.remove("children");
            }
            ObjectMapper mapper = new ObjectMapper();
            mapper.setDateFormat(jsonDateFormat);
            return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(objMap);
        } catch (Exception e) { throw new RuntimeException(e); }
    }
    // Converts a single json object into an ActiveJdbc Model
    public <T extends CecilModel> T toObj(String json) {
        try {
            Map<String, Object> objMap = toMap(json);
            convertDatesToTimestamp(objMap);
            return this.fromMap(objMap);
        } catch (Exception e) { throw new RuntimeException(e); }
    }

    // STATIC CONVERTERS FOR COLLECTIONS OF OBJECTS
    // Convert an ActiveJdbc LazyList Collection to a JSON string
    public static <T extends Model> String toJson(Collection<T> objCollection) {
        //objCollection.load();
        StringBuffer json = new StringBuffer("[ ");
        for (T obj: objCollection) {
            if (CecilModel.class.isInstance(obj)) {
                json.append(((CecilModel)obj).toJson() + ",");
            } else {
                try {
                    json.append(new ObjectMapper().writeValueAsString(obj));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
        return json.substring(0, json.length()-1) + "]";
    }
    // Converts an array of json objects into a list of ActiveJdbc Models
    public static <T extends Model> List<T> toObjList(String json, Class<T> cls) {
        List<T> results = new LinkedList<T>();
        try {
            List<Map<String, Object>> objMapList = toMaps(json);
            for (Map<String, Object> objMap: objMapList) {
                convertDatesToTimestamp(objMap);
                T obj = cls.newInstance().fromMap(objMap);
                results.add(obj);
            }
        } catch (Exception e) { throw new RuntimeException(e); } 
        return results;
    }


    // Converts a single json object into a map of key:value pairs
    private static Map<String, Object> toMap(String json) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(json, mapType);
        } catch (IOException e) { throw new RuntimeException(e); }
    }
    // Converts an array of json objects into a list of maps 
    private static List<Map<String, Object>> toMaps(String json) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(json, listMapType);
        } catch (IOException e) { throw new RuntimeException(e); } 
    }
    // checks for Strings that are formatted in 'yyyy-MM-ddTHH:mm:ss.SSSZ' format
    // if found it will replace the String value with a java.sql.Timestamp value in the objMap
    private static final Map<String,Object> convertDatesToTimestamp(Map<String, Object> objMap) {
        // attempt to convert all dates to java.sql.Timestamp
        for (String key: objMap.keySet()) {
            Object value = objMap.get(key);
            System.out.println("Checking if '" + key + "=" + (value == null ? "null" : value.toString()) +"' is a date...");
            if (value instanceof String && ((String) value).matches("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z$")) {
                String valuestr = (String) value;
                System.out.println("DATE FOUND FOR '" + key + "' " + value);
                objMap.put(key, Timestamp.valueOf(ZonedDateTime.parse(valuestr).toLocalDateTime()));
            }
        }
        return objMap;
    }

    public static ValidationBuilder<?> validateAbsenceOf(String ... attributes) {
        return validateWith(new ValidatorAdapter() {

            @Override
            public void validate(Model m) {
                boolean containsAttribute = false;

                for(String attribute:attributes) {

                    if(m.attributeNames().contains(attribute)) {
                        //model contains attribute, invalidate now !
                        m.addValidator(this, attribute);
                        break;
                    }
                }
            }
        });
    }
}

我们的控制器

package com.brookdale.controller;

import static spark.Spark.get;
import static spark.Spark.post;
import static spark.Spark.put;

import org.codehaus.jackson.map.ObjectMapper;

import com.brookdale.model.RecurringItem;
import com.brookdale.model.activejdbc.CecilModel;
import com.brookdale.security.bo.User;
import com.brookdale.service.RecurringItemService;

public class RecurringItemController {

    public RecurringItemController(final RecurringItemService service) {
        // get list of recurring items based on the agreementid
        get("/api/recurring/list/:agreementid", (req,res)-> { 
            String all = req.queryParams("all");
            Long agreementid = Long.parseLong(req.params(":agreementid"));
            //return RecurringItemAPI.searchByAgreement(Long.parseLong(req.params(":agreementid"))).toJson(true); 
            return CecilModel.toJson(service.findByAgreementId(agreementid, all != null)); 

        });
        // insert
        post("/api/recurring/save", (req,res)-> { 
            //RecurringItem ri = ActiveJdbcJson.toObj(req.body(), RecurringItem.class);
            RecurringItem ri = new RecurringItem().toObj(req.body());
            // set unitqty to '1' since the field is not nullable, but not used
            if (ri.getUnitQty() == null)
                ri.setUnitQty(1);
            System.out.println("ri to insert: " + new ObjectMapper().writeValueAsString(ri));
            return service.saveRecurringItem(ri, (User) req.attribute("user")).toJson(); 
        });
        // update
        put("/api/recurring/save", (req,res)-> { 
            //RecurringItem ri = ActiveJdbcJson.toObj(req.body(), RecurringItem.class);
            RecurringItem ri = new RecurringItem().toObj(req.body());
            System.out.println("ri to update: " + new ObjectMapper().writeValueAsString(ri));
            return service.saveRecurringItem(ri, (User) req.attribute("user")).toJson(); 
        });

    }

}

调用我们的服务层进行保存。

package com.brookdale.service;

import org.javalite.activejdbc.LazyList;
import org.javalite.activejdbc.Model;

import com.brookdale.model.RecurringItem;
import com.brookdale.security.bo.User;

public class RecurringItemService {

    public RecurringItemService() { }

    public LazyList<Model> findByAgreementId(Long agreementId, boolean includeAll) {
        if (includeAll)
            return RecurringItem.find("contactAgreementID = ?", agreementId).orderBy("effstartdate desc");
        else
            return RecurringItem.find("contactAgreementID = ? and effenddate is null", agreementId).orderBy("effstartdate desc");
    }
    public RecurringItem saveRecurringItem(RecurringItem ri, User user) {
        ri.saveIt(user);
        return ri;
    }

}