2017-03-24 3 views
0
@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")をやることができますが、値を設定する代わりにこれが好きです。フォームから送信された値をactivejdbcに保存するにはどうすればよいですか?

+0

ActiveWebを使用していないことがわかりますが、どのような技術を使ってWebリクエストを処理していますか? Requestクラスとは何ですか? – ipolevoy

+0

@ipolevoy java spark – kittu

+0

@ipolevoy 'spark'のリクエストクラスです。リクエストクラスのリンクは次のとおりです。http://javadoc.io/doc/com.sparkjava/spark-core/2.5.4 – kittu

答えて

1

は、私には、Spark(私はActiveWebの著者だ)に慣れていないんだけど、あなたは/クローズの接続を開くためにフィルタを使用する代わりに、あなたのサービスクラスを汚染することができます場合は、

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

さらに、

Post post = new Post(); 
post.fromMap(parameters); 
if(post.save()){ 
    //display success 
}else{ 
    Errors errors = post.errors(); 
    //display errors 
} 

これはActiveWebの例ですが、あまりにもスパークをお届けします: あなたはその後、これを行う、java.util.Mapにあなたのリクエストパラメータを変換することができます

+0

私はこの例を見ましたが、理解できませんでした。リクエストボディ/パラメータをマップに変換するにはどうすればよいですか?私は 'request.body()'の中にデータを持っていますので、 'post.fromMap(request.body())'を実行します?? – kittu

+1

これはActiveJDBCに関連していないため、Sparkのドキュメントを調べる必要があります。ActiveWebを使用している場合、これは1つのライナーです。 'post.fromMap(parameters1st());' – ipolevoy

1

私が正しく理解している場合は、POSTリクエストから値を取得し、ActiveJDBCを使用してそれらの値を保存する方法を検討しています。私もかなり新しく、私たちはアプリの初めの段階ですが、ActiveJDBCでSparkJavaを使用しています。

例は実際のコードですが、私は単純化する時間がありませんでした。しかし基本的には、モデルクラス用のPOJOを作成しました。最初はorg.javalite.activejdbc.Modelを拡張しましたが、監査フィールド(作成、更新ユーザー/時間)を処理し、JSONからの変換を支援する必要があったので、これをCecilModelというカスタムクラスで拡張しました。しかし、CecilModelはModelクラスを拡張しています。

リクエストを受け取るコントローラがあります。リクエストは、モデルクラスのフィールド名と一致する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."); 
     } 

    } 

} 

カスタムモデルクラスを拡張します。これにより、実際の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; 
    } 

} 
+0

あなたの投稿は3月からのものだと思います。 –

関連する問題