Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.20 KB | None | 0 0
  1. package com.mlpt.bvnextgen.bo.newforwardlooking;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.List;
  6.  
  7. import org.hibernate.Query;
  8. import org.json.simple.JSONObject;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Service;
  13.  
  14. import com.mlpt.bvnextgen.MasterGeneralConstants;
  15. import com.mlpt.bvnextgen.annotation.Info;
  16. import com.mlpt.bvnextgen.annotation.InfoIn;
  17. import com.mlpt.bvnextgen.annotation.InfoOut;
  18. import com.mlpt.bvnextgen.bo.core.DataTransaction;
  19. import com.mlpt.bvnextgen.bo.core.DefaultDataTransaction;
  20. import com.mlpt.bvnextgen.bo.core.ValidationUtil;
  21. import com.mlpt.bvnextgen.dao.ConfigurationDao;
  22. import com.mlpt.bvnextgen.dao.TempRegressionCalcResultDao;
  23. import com.mlpt.bvnextgen.entity.TempRegressionCalcResult;
  24. import com.mlpt.bvnextgen.entity.TempRegressionCalcResultDetail;
  25.  
  26. @InfoIn(value={
  27. @Info(name="sessionId", description="session Id", type="String.class"),
  28. @Info(name="subConfig", description="Sub Config Variable", type="String.class"),
  29. @Info(name="regressionType", description="period", type="String.class"),
  30. @Info(name="datetime", description="datetime", type="String.class"),
  31. @Info(name="userId", description="userId", type="Long.class")
  32. })
  33. @InfoOut(value={
  34. @Info(name="rSquared", description="rSquared", type="Double.class"),
  35. @Info(name="coefVarA", description="coefVarA", type="Double.class"),
  36. @Info(name="coefVarB1", description="coefVarB1", type="Double.class"),
  37. @Info(name="coefVarB2", description="coefVarB2", type="Double.class"),
  38. @Info(name="coefVarB3", description="coefVarB3", type="Double.class"),
  39. @Info(name="dataValueList", description="DataValueList", type="List.class"),
  40. @Info(name="calcResultList", description="calcResultList", type="List.class")
  41. })
  42.  
  43. @Service
  44. public class GetRegressionCalculationResultBySessionAndType extends DefaultDataTransaction implements DataTransaction{
  45.  
  46. Logger log = LoggerFactory.getLogger(GetRegressionCalculationResultBySessionAndType.class);
  47.  
  48. @Autowired
  49. TempRegressionCalcResultDao tempRegressionCalcResultDao;
  50.  
  51. @Autowired
  52. ConfigurationDao configureDao;
  53.  
  54. @SuppressWarnings({ "unchecked", "rawtypes", "unused" })
  55. @Override
  56. public JSONObject prepare(JSONObject serviceInput) throws Exception {
  57.  
  58. ValidationUtil.valBlankOrNull(serviceInput, "sessionId");
  59. ValidationUtil.valBlankOrNull(serviceInput, "subConfig");
  60. ValidationUtil.valBlankOrNull(serviceInput, "regressionType");
  61. ValidationUtil.valBlankOrNull(serviceInput, "datetime");
  62. ValidationUtil.valBlankOrNull(serviceInput, "userId");
  63. return serviceInput;
  64.  
  65. }
  66. @SuppressWarnings({ "unchecked", "unused", "rawtypes" })
  67. @Override
  68. public JSONObject process(JSONObject serviceInput) throws Exception {
  69. JSONObject outputJson = new JSONObject();
  70.  
  71. String sessionId = serviceInput.get("sessionId").toString();
  72. String subConfig = serviceInput.get("subConfig").toString();
  73. String regressionType = serviceInput.get("regressionType").toString();
  74. String datetime = serviceInput.get("datetime").toString();
  75. Long userId = Long.valueOf(serviceInput.get("userId").toString());
  76.  
  77. List<Object[]> result = null;
  78. List<JSONObject> dataValueList = new ArrayList<JSONObject>();
  79. List<JSONObject> calcResultList = new ArrayList<JSONObject>();
  80. List<JSONObject> calcResultListRev = new ArrayList<JSONObject>();
  81.  
  82. String calcDatetime = new String();
  83. Double rSquared = new Double(0);
  84. Double coefVarA = new Double(0);
  85. Double coefVarB1 = new Double(0);
  86. Double coefVarB2 = new Double(0);
  87. Double coefVarB3 = new Double(0);
  88.  
  89. if (!regressionType.equals(MasterGeneralConstants.REGRESSION_MODEL_LINEAR) &&
  90. !regressionType.equals(MasterGeneralConstants.REGRESSION_MODEL_QUADRATIC) &&
  91. !regressionType.equals(MasterGeneralConstants.REGRESSION_MODEL_CUBIC) &&
  92. !regressionType.equals(MasterGeneralConstants.REGRESSION_MODEL_EXPONENTIAL)) {
  93.  
  94. throw new Exception("Invalid Regression Type");
  95. }
  96.  
  97.  
  98.  
  99. // Mendapatkan list nilai yang digunakan untuk perhitungan regresi
  100.  
  101. //DATA AWAL
  102. StringBuilder queryBuilder = new StringBuilder();
  103. queryBuilder.append(" SELECT period, val1 FROM ").append(MasterGeneralConstants.TABLE_TT_DATA_MODEL)
  104. .append(" WHERE session_id = :sessionId AND var1 = :subConfig ")
  105. .append(" ORDER BY period ASC ");
  106.  
  107. Query q = tempRegressionCalcResultDao.createSQLQuery(queryBuilder.toString());
  108. q.setParameter("sessionId", sessionId);
  109. q.setParameter("subConfig", subConfig);
  110. result = q.list();
  111.  
  112. tempRegressionCalcResultDao.closeSessionCreateQuery();
  113.  
  114. int tempPeriodNo = 1;
  115. Iterator iter = result.iterator();
  116. while(iter.hasNext()){
  117. Object[] obj = (Object[]) iter.next();
  118. JSONObject tempConfig = new JSONObject();
  119.  
  120. String periodValue = obj[0] != null ? obj[0].toString() : null;
  121. Double varValue = obj[1] != null ? Double.valueOf(obj[1].toString()) : null;
  122.  
  123. tempConfig.put("periodNo", tempPeriodNo);
  124. tempConfig.put("periodValue", periodValue);
  125. tempConfig.put("varValue", varValue);
  126.  
  127. dataValueList.add(tempConfig);
  128.  
  129. tempPeriodNo++;
  130. }
  131.  
  132.  
  133. //Calc Result List
  134. Integer historicalSize = Integer.valueOf(getHistoricalSize());
  135. if(historicalSize == 0){
  136. outputJson.put("status", "F");
  137. return outputJson;
  138. }
  139.  
  140. Integer totalDataRegression = historicalSize * 12;
  141. System.out.println("totalDataRegression : "+totalDataRegression);
  142.  
  143. queryBuilder = new StringBuilder();
  144. queryBuilder.append(" SELECT period_no, var_y FROM ").append(TempRegressionCalcResultDetail.TABLE_NAME)
  145. .append(" WHERE session_id = :sessionId AND var_config = :subConfig AND regression_type = :regressionType ")
  146. // .append(" AND period_no BETWEEN 1 AND 36 ");
  147. .append(" ORDER BY period_no DESC ")
  148. .append(" limit :limit ");
  149.  
  150.  
  151. q = tempRegressionCalcResultDao.createSQLQuery(queryBuilder.toString());
  152. q.setParameter("sessionId", sessionId);
  153. q.setParameter("subConfig", subConfig);
  154. q.setParameter("regressionType", regressionType);
  155. q.setParameter("limit", totalDataRegression);
  156. result = q.list();
  157.  
  158. tempRegressionCalcResultDao.closeSessionCreateQuery();
  159.  
  160. iter = result.iterator();
  161. while(iter.hasNext()){
  162. Object[] obj = (Object[]) iter.next();
  163. JSONObject tempConfig = new JSONObject();
  164.  
  165. Long periodValue = obj[0] != null ? Long.valueOf(obj[0].toString()) : null;
  166. Double varValue = obj[1] != null ? Double.valueOf(obj[1].toString()) : null;
  167.  
  168. tempConfig.put("periodNo", periodValue);
  169. tempConfig.put("varValue", varValue);
  170.  
  171. calcResultList.add(tempConfig);
  172.  
  173. tempPeriodNo++;
  174. }
  175. //reverse
  176. for(int i=calcResultList.size()-1 ; i>=0 ; i-- ){
  177. calcResultListRev.add(calcResultList.get(i));
  178. }
  179.  
  180.  
  181.  
  182.  
  183. //Coefisien
  184. queryBuilder = new StringBuilder();
  185. queryBuilder.append(" SELECT calc_datetime, r_squared, coef_var_a, coef_var_b1, coef_var_b2, coef_var_b3 ")
  186. .append(" FROM ").append(TempRegressionCalcResult.TABLE_NAME)
  187. .append(" WHERE session_id = :sessionId AND var_config = :subConfig AND regression_type = :regressionType ");
  188.  
  189. q = tempRegressionCalcResultDao.createSQLQuery(queryBuilder.toString());
  190. q.setParameter("sessionId", sessionId);
  191. q.setParameter("subConfig", subConfig);
  192. q.setParameter("regressionType", regressionType);
  193. result = q.list();
  194.  
  195. tempRegressionCalcResultDao.closeSessionCreateQuery();
  196.  
  197. iter = result.iterator();
  198. while(iter.hasNext()){
  199. Object[] obj = (Object[]) iter.next();
  200.  
  201. calcDatetime = obj[0] != null ? obj[0].toString() : null;
  202. rSquared = obj[1] != null ? Double.valueOf(obj[1].toString()) : 0D;
  203. coefVarA = obj[2] != null ? Double.valueOf(obj[2].toString()) : 0D;
  204. coefVarB1 = obj[3] != null ? Double.valueOf(obj[3].toString()) : 0D;
  205. coefVarB2 = obj[4] != null ? Double.valueOf(obj[4].toString()) : 0D;
  206. coefVarB3 = obj[5] != null ? Double.valueOf(obj[5].toString()) : 0D;
  207. }
  208.  
  209.  
  210. outputJson.put("dataValueList", dataValueList);
  211. outputJson.put("calcResultList", calcResultListRev);
  212. outputJson.put("calcDatetime", calcDatetime);
  213. outputJson.put("rSquared", rSquared);
  214. outputJson.put("coefVarA", coefVarA);
  215. outputJson.put("coefVarB1", coefVarB1);
  216. outputJson.put("coefVarB2", coefVarB2);
  217. outputJson.put("coefVarB3", coefVarB3);
  218.  
  219. return outputJson;
  220. }
  221.  
  222.  
  223. public String getHistoricalSize(){
  224.  
  225. List<String> queryResultFindHistoricalSize = new ArrayList<String>();
  226. try {
  227.  
  228. Query findBreak = configureDao.createQuery("Select configValue "
  229. + "from com.mlpt.bvnextgen.entity.Configuration "
  230. + "where configMenu = :configMenu and subconfig = :subconfig");
  231. findBreak.setParameter("configMenu", MasterGeneralConstants.FORWARD_LOOKING);
  232. findBreak.setParameter("subconfig", "historicalSize");
  233.  
  234. queryResultFindHistoricalSize = findBreak.list();
  235. configureDao.closeSessionCreateQuery();
  236.  
  237. System.out.println("queryResultFindHistoricalSize : " + queryResultFindHistoricalSize.get(0));
  238. String historicalSize = String.valueOf(queryResultFindHistoricalSize.get(0));
  239.  
  240. return historicalSize;
  241.  
  242. } catch (Exception e) {
  243.  
  244. e.printStackTrace();
  245. return "0";
  246. }
  247.  
  248. }
  249.  
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement