Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mlpt.bvnextgen.bo.newforwardlooking;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import org.hibernate.Query;
- import org.json.simple.JSONObject;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import com.mlpt.bvnextgen.MasterGeneralConstants;
- import com.mlpt.bvnextgen.annotation.Info;
- import com.mlpt.bvnextgen.annotation.InfoIn;
- import com.mlpt.bvnextgen.annotation.InfoOut;
- import com.mlpt.bvnextgen.bo.core.DataTransaction;
- import com.mlpt.bvnextgen.bo.core.DefaultDataTransaction;
- import com.mlpt.bvnextgen.bo.core.ValidationUtil;
- import com.mlpt.bvnextgen.dao.ConfigurationDao;
- import com.mlpt.bvnextgen.dao.TempRegressionCalcResultDao;
- import com.mlpt.bvnextgen.entity.TempRegressionCalcResult;
- import com.mlpt.bvnextgen.entity.TempRegressionCalcResultDetail;
- @InfoIn(value={
- @Info(name="sessionId", description="session Id", type="String.class"),
- @Info(name="subConfig", description="Sub Config Variable", type="String.class"),
- @Info(name="regressionType", description="period", type="String.class"),
- @Info(name="datetime", description="datetime", type="String.class"),
- @Info(name="userId", description="userId", type="Long.class")
- })
- @InfoOut(value={
- @Info(name="rSquared", description="rSquared", type="Double.class"),
- @Info(name="coefVarA", description="coefVarA", type="Double.class"),
- @Info(name="coefVarB1", description="coefVarB1", type="Double.class"),
- @Info(name="coefVarB2", description="coefVarB2", type="Double.class"),
- @Info(name="coefVarB3", description="coefVarB3", type="Double.class"),
- @Info(name="dataValueList", description="DataValueList", type="List.class"),
- @Info(name="calcResultList", description="calcResultList", type="List.class")
- })
- @Service
- public class GetRegressionCalculationResultBySessionAndType extends DefaultDataTransaction implements DataTransaction{
- Logger log = LoggerFactory.getLogger(GetRegressionCalculationResultBySessionAndType.class);
- @Autowired
- TempRegressionCalcResultDao tempRegressionCalcResultDao;
- @Autowired
- ConfigurationDao configureDao;
- @SuppressWarnings({ "unchecked", "rawtypes", "unused" })
- @Override
- public JSONObject prepare(JSONObject serviceInput) throws Exception {
- ValidationUtil.valBlankOrNull(serviceInput, "sessionId");
- ValidationUtil.valBlankOrNull(serviceInput, "subConfig");
- ValidationUtil.valBlankOrNull(serviceInput, "regressionType");
- ValidationUtil.valBlankOrNull(serviceInput, "datetime");
- ValidationUtil.valBlankOrNull(serviceInput, "userId");
- return serviceInput;
- }
- @SuppressWarnings({ "unchecked", "unused", "rawtypes" })
- @Override
- public JSONObject process(JSONObject serviceInput) throws Exception {
- JSONObject outputJson = new JSONObject();
- String sessionId = serviceInput.get("sessionId").toString();
- String subConfig = serviceInput.get("subConfig").toString();
- String regressionType = serviceInput.get("regressionType").toString();
- String datetime = serviceInput.get("datetime").toString();
- Long userId = Long.valueOf(serviceInput.get("userId").toString());
- List<Object[]> result = null;
- List<JSONObject> dataValueList = new ArrayList<JSONObject>();
- List<JSONObject> calcResultList = new ArrayList<JSONObject>();
- List<JSONObject> calcResultListRev = new ArrayList<JSONObject>();
- String calcDatetime = new String();
- Double rSquared = new Double(0);
- Double coefVarA = new Double(0);
- Double coefVarB1 = new Double(0);
- Double coefVarB2 = new Double(0);
- Double coefVarB3 = new Double(0);
- if (!regressionType.equals(MasterGeneralConstants.REGRESSION_MODEL_LINEAR) &&
- !regressionType.equals(MasterGeneralConstants.REGRESSION_MODEL_QUADRATIC) &&
- !regressionType.equals(MasterGeneralConstants.REGRESSION_MODEL_CUBIC) &&
- !regressionType.equals(MasterGeneralConstants.REGRESSION_MODEL_EXPONENTIAL)) {
- throw new Exception("Invalid Regression Type");
- }
- // Mendapatkan list nilai yang digunakan untuk perhitungan regresi
- //DATA AWAL
- StringBuilder queryBuilder = new StringBuilder();
- queryBuilder.append(" SELECT period, val1 FROM ").append(MasterGeneralConstants.TABLE_TT_DATA_MODEL)
- .append(" WHERE session_id = :sessionId AND var1 = :subConfig ")
- .append(" ORDER BY period ASC ");
- Query q = tempRegressionCalcResultDao.createSQLQuery(queryBuilder.toString());
- q.setParameter("sessionId", sessionId);
- q.setParameter("subConfig", subConfig);
- result = q.list();
- tempRegressionCalcResultDao.closeSessionCreateQuery();
- int tempPeriodNo = 1;
- Iterator iter = result.iterator();
- while(iter.hasNext()){
- Object[] obj = (Object[]) iter.next();
- JSONObject tempConfig = new JSONObject();
- String periodValue = obj[0] != null ? obj[0].toString() : null;
- Double varValue = obj[1] != null ? Double.valueOf(obj[1].toString()) : null;
- tempConfig.put("periodNo", tempPeriodNo);
- tempConfig.put("periodValue", periodValue);
- tempConfig.put("varValue", varValue);
- dataValueList.add(tempConfig);
- tempPeriodNo++;
- }
- //Calc Result List
- Integer historicalSize = Integer.valueOf(getHistoricalSize());
- if(historicalSize == 0){
- outputJson.put("status", "F");
- return outputJson;
- }
- Integer totalDataRegression = historicalSize * 12;
- System.out.println("totalDataRegression : "+totalDataRegression);
- queryBuilder = new StringBuilder();
- queryBuilder.append(" SELECT period_no, var_y FROM ").append(TempRegressionCalcResultDetail.TABLE_NAME)
- .append(" WHERE session_id = :sessionId AND var_config = :subConfig AND regression_type = :regressionType ")
- // .append(" AND period_no BETWEEN 1 AND 36 ");
- .append(" ORDER BY period_no DESC ")
- .append(" limit :limit ");
- q = tempRegressionCalcResultDao.createSQLQuery(queryBuilder.toString());
- q.setParameter("sessionId", sessionId);
- q.setParameter("subConfig", subConfig);
- q.setParameter("regressionType", regressionType);
- q.setParameter("limit", totalDataRegression);
- result = q.list();
- tempRegressionCalcResultDao.closeSessionCreateQuery();
- iter = result.iterator();
- while(iter.hasNext()){
- Object[] obj = (Object[]) iter.next();
- JSONObject tempConfig = new JSONObject();
- Long periodValue = obj[0] != null ? Long.valueOf(obj[0].toString()) : null;
- Double varValue = obj[1] != null ? Double.valueOf(obj[1].toString()) : null;
- tempConfig.put("periodNo", periodValue);
- tempConfig.put("varValue", varValue);
- calcResultList.add(tempConfig);
- tempPeriodNo++;
- }
- //reverse
- for(int i=calcResultList.size()-1 ; i>=0 ; i-- ){
- calcResultListRev.add(calcResultList.get(i));
- }
- //Coefisien
- queryBuilder = new StringBuilder();
- queryBuilder.append(" SELECT calc_datetime, r_squared, coef_var_a, coef_var_b1, coef_var_b2, coef_var_b3 ")
- .append(" FROM ").append(TempRegressionCalcResult.TABLE_NAME)
- .append(" WHERE session_id = :sessionId AND var_config = :subConfig AND regression_type = :regressionType ");
- q = tempRegressionCalcResultDao.createSQLQuery(queryBuilder.toString());
- q.setParameter("sessionId", sessionId);
- q.setParameter("subConfig", subConfig);
- q.setParameter("regressionType", regressionType);
- result = q.list();
- tempRegressionCalcResultDao.closeSessionCreateQuery();
- iter = result.iterator();
- while(iter.hasNext()){
- Object[] obj = (Object[]) iter.next();
- calcDatetime = obj[0] != null ? obj[0].toString() : null;
- rSquared = obj[1] != null ? Double.valueOf(obj[1].toString()) : 0D;
- coefVarA = obj[2] != null ? Double.valueOf(obj[2].toString()) : 0D;
- coefVarB1 = obj[3] != null ? Double.valueOf(obj[3].toString()) : 0D;
- coefVarB2 = obj[4] != null ? Double.valueOf(obj[4].toString()) : 0D;
- coefVarB3 = obj[5] != null ? Double.valueOf(obj[5].toString()) : 0D;
- }
- outputJson.put("dataValueList", dataValueList);
- outputJson.put("calcResultList", calcResultListRev);
- outputJson.put("calcDatetime", calcDatetime);
- outputJson.put("rSquared", rSquared);
- outputJson.put("coefVarA", coefVarA);
- outputJson.put("coefVarB1", coefVarB1);
- outputJson.put("coefVarB2", coefVarB2);
- outputJson.put("coefVarB3", coefVarB3);
- return outputJson;
- }
- public String getHistoricalSize(){
- List<String> queryResultFindHistoricalSize = new ArrayList<String>();
- try {
- Query findBreak = configureDao.createQuery("Select configValue "
- + "from com.mlpt.bvnextgen.entity.Configuration "
- + "where configMenu = :configMenu and subconfig = :subconfig");
- findBreak.setParameter("configMenu", MasterGeneralConstants.FORWARD_LOOKING);
- findBreak.setParameter("subconfig", "historicalSize");
- queryResultFindHistoricalSize = findBreak.list();
- configureDao.closeSessionCreateQuery();
- System.out.println("queryResultFindHistoricalSize : " + queryResultFindHistoricalSize.get(0));
- String historicalSize = String.valueOf(queryResultFindHistoricalSize.get(0));
- return historicalSize;
- } catch (Exception e) {
- e.printStackTrace();
- return "0";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement