View Javadoc
1   package org.kuali.ole.loaders.describe.service.impl;
2   
3   import com.sun.jersey.api.core.HttpContext;
4   import org.apache.commons.lang.StringUtils;
5   import org.apache.log4j.Logger;
6   import org.codehaus.jettison.json.JSONArray;
7   import org.codehaus.jettison.json.JSONException;
8   import org.codehaus.jettison.json.JSONObject;
9   import org.kuali.ole.describe.bo.OleStatisticalSearchingCodes;
10  import org.kuali.ole.loaders.common.bo.OLELoaderImportResponseBo;
11  import org.kuali.ole.loaders.common.bo.OLELoaderResponseBo;
12  import org.kuali.ole.loaders.common.constants.OLELoaderConstants;
13  import org.kuali.ole.loaders.common.service.OLELoaderService;
14  import org.kuali.ole.loaders.common.service.impl.OLELoaderServiceImpl;
15  import org.kuali.ole.loaders.describe.bo.OLEStatisticalSearchingCodeBo;
16  import org.kuali.ole.loaders.describe.service.OLEStatisticalSearchingCodeLoaderHelperService;
17  import org.kuali.ole.loaders.describe.service.OLEStatisticalSearchingCodeLoaderService;
18  import org.kuali.rice.krad.service.BusinessObjectService;
19  import org.kuali.rice.krad.service.KRADServiceLocator;
20  
21  import java.util.ArrayList;
22  import java.util.Date;
23  import java.util.List;
24  
25  /**
26   * Created by sheiksalahudeenm on 27/3/15.
27   */
28  public class OLEStatisticalSearchingCodeLoaderServiceImpl implements OLEStatisticalSearchingCodeLoaderService {
29  
30      private static final Logger LOG = Logger.getLogger(OLEStatisticalSearchingCodeLoaderServiceImpl.class);
31      private OLEStatisticalSearchingCodeLoaderHelperService oleStatisticalSearchingCodeLoaderHelperService;
32      private OLELoaderService oleLoaderService;
33      private BusinessObjectService businessObjectService;
34  
35      public OLEStatisticalSearchingCodeLoaderHelperService getOleStatisticalSearchingCodeLoaderHelperService() {
36          if(oleStatisticalSearchingCodeLoaderHelperService == null){
37              oleStatisticalSearchingCodeLoaderHelperService = new OLEStatisticalSearchingCodeLoaderHelperServiceImpl();
38          }
39          return oleStatisticalSearchingCodeLoaderHelperService;
40      }
41  
42      public void setOleStatisticalSearchingCodeLoaderHelperService(OLEStatisticalSearchingCodeLoaderHelperService oleStatisticalSearchingCodeLoaderHelperService) {
43          this.oleStatisticalSearchingCodeLoaderHelperService = oleStatisticalSearchingCodeLoaderHelperService;
44      }
45  
46      public OLELoaderService getOleLoaderService() {
47          if(oleLoaderService == null ){
48              oleLoaderService = new OLELoaderServiceImpl();
49          }
50          return oleLoaderService;
51      }
52  
53      public void setOleLoaderService(OLELoaderService oleLoaderService) {
54          this.oleLoaderService = oleLoaderService;
55      }
56  
57      public BusinessObjectService getBusinessObjectService() {
58          if(businessObjectService == null){
59              businessObjectService = KRADServiceLocator.getBusinessObjectService();
60          }
61          return businessObjectService;
62      }
63  
64      public void setBusinessObjectService(BusinessObjectService businessObjectService) {
65          this.businessObjectService = businessObjectService;
66      }
67  
68      @Override
69      public Object importStatisticalSearchingCode(String bodyContent, HttpContext context) {
70          LOG.info("Inside importStatisticalSearchingCode method.");
71          OLELoaderImportResponseBo oleLoaderImportResponseBo = new OLELoaderImportResponseBo();
72          List<Integer> rejectStatisticalSearchingCodeList = new ArrayList<Integer>();
73          List<JSONObject> createdStatisticalSearchingCodeObject = new ArrayList<JSONObject>();
74          JSONObject requestJsonObject = getOleLoaderService().getJsonObjectFromString(bodyContent);
75          boolean validObject = false;
76          if(requestJsonObject != null) {
77              if (requestJsonObject.has("items")) {
78                  String items = getOleLoaderService().getStringValueFromJsonObject(requestJsonObject, "items");
79                  if (StringUtils.isNotBlank(items)) {
80                      JSONArray statisticalSearchingCodeJsonArray = getOleLoaderService().getJsonArrayFromString(items);
81                      for (int index = 0; index < statisticalSearchingCodeJsonArray.length(); index ++) {
82                          JSONObject jsonObject = null;
83                          OleStatisticalSearchingCodes statisticalSearchingCode = new OleStatisticalSearchingCodes();
84                          try {
85                              jsonObject = (JSONObject)statisticalSearchingCodeJsonArray.get(index);
86                              if(jsonObject != null){
87                                  if(jsonObject.has("name")){
88                                      String name = getOleLoaderService().getStringValueFromJsonObject(jsonObject,"name");
89                                      if(StringUtils.isNotBlank(name)){
90                                          statisticalSearchingCode.setStatisticalSearchingName(name);
91                                          validObject = true;
92                                      }
93                                  }
94                                  if(jsonObject.has("code")){
95                                      String code = getOleLoaderService().getStringValueFromJsonObject(jsonObject,"code");
96                                      if(StringUtils.isNotBlank(code)){
97                                          statisticalSearchingCode.setStatisticalSearchingCode(code);
98                                          validObject = true;
99                                      }
100                                 }
101 
102                                 if(jsonObject.has("source")){
103                                     String source = getOleLoaderService().getStringValueFromJsonObject(jsonObject,"source");
104                                     if(StringUtils.isNotBlank(source)){
105                                         statisticalSearchingCode.setSource(source);
106                                         validObject = true;
107                                     }
108                                 }
109                                 if(jsonObject.has("active")){
110                                     try{
111                                         boolean active = Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(jsonObject, "active"));
112                                         statisticalSearchingCode.setActive(active);
113                                         validObject = true;
114                                     }catch(Exception e){
115                                         e.printStackTrace();
116                                         rejectStatisticalSearchingCodeList.add(index+1);
117                                         continue;
118                                     }
119 
120                                 }
121 
122                                 if(jsonObject.has("sourceDate")){
123                                     String sourceDate = getOleLoaderService().getStringValueFromJsonObject(jsonObject,"sourceDate");
124                                     if(StringUtils.isNotBlank(sourceDate)){
125                                         try{
126                                             Date date = OLELoaderConstants.DATE_FORMAT.parse(sourceDate);
127                                             statisticalSearchingCode.setSourceDate(new java.sql.Date(date.getTime()));
128                                             validObject = true;
129                                         }catch(Exception e){
130                                             e.printStackTrace();
131                                             rejectStatisticalSearchingCodeList.add(index+1);
132                                             continue;
133                                         }
134                                     }
135                                 }
136 
137                             }
138                             if(statisticalSearchingCode != null && validObject){
139                                 if(getOleStatisticalSearchingCodeLoaderHelperService().getStatisticalSearchingCodeByCode(statisticalSearchingCode.getStatisticalSearchingCode()) == null){
140                                     try {
141                                         statisticalSearchingCode = getBusinessObjectService().save(statisticalSearchingCode);
142                                         createdStatisticalSearchingCodeObject.add((JSONObject)getOleStatisticalSearchingCodeLoaderHelperService().formStatisticalSearchingCodeExportResponse(statisticalSearchingCode, OLELoaderConstants.OLELoaderContext.STAT_SEARCH_CODE,
143                                                 context.getRequest().getRequestUri().toASCIIString(), false));
144                                     } catch (Exception e) {
145                                         rejectStatisticalSearchingCodeList.add(index+1);
146                                         continue;
147                                     }
148                                 }else{
149                                     rejectStatisticalSearchingCodeList.add(index+1);
150                                     continue;
151                                 }
152 
153 
154                             }else{
155                                 rejectStatisticalSearchingCodeList.add(index+1);
156                                 continue;
157                             }
158                         } catch (JSONException e) {
159                             e.printStackTrace();
160                             rejectStatisticalSearchingCodeList.add(index+1);
161                             continue;
162                         }
163                     }
164                 }else{
165                     return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.BAD_REQUEST, OLELoaderConstants.OLEloaderStatus.BAD_REQUEST);
166                 }
167 
168             }
169         }else{
170             return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.BAD_REQUEST, OLELoaderConstants.OLEloaderStatus.BAD_REQUEST);
171         }
172         oleLoaderImportResponseBo.setOleRejectedBos(rejectStatisticalSearchingCodeList);
173         oleLoaderImportResponseBo.setOleCreatedBos(createdStatisticalSearchingCodeObject);
174         return oleLoaderImportResponseBo;
175     }
176 
177     @Override
178     public OLELoaderResponseBo updateStatisticalSearchingCodeById(String statisticalSearchingCodeId, String bodyContent, HttpContext context) {
179         LOG.info("Inside updateStatisticalSearchingCodeById method.");
180         OLEStatisticalSearchingCodeBo oleStatisticalSearchingCodeBo = new OLEStatisticalSearchingCodeBo();
181         JSONObject jsonObject = getOleLoaderService().getJsonObjectFromString(bodyContent);
182         boolean validObject = false;
183         if (jsonObject != null) {
184             if (jsonObject.has("name")) {
185                 String name = getOleLoaderService().getStringValueFromJsonObject(jsonObject, "name");
186                 if (StringUtils.isNotBlank(name)) {
187                     oleStatisticalSearchingCodeBo.setStatisticalSearchingName(name);
188                     validObject = true;
189                 }
190             }
191 
192             if(jsonObject.has("source")){
193                 String source = getOleLoaderService().getStringValueFromJsonObject(jsonObject,"source");
194                 if(StringUtils.isNotBlank(source)){
195                     oleStatisticalSearchingCodeBo.setSource(source);
196                     validObject = true;
197                 }
198             }
199 
200             if(jsonObject.has("active")){
201                 try{
202                     boolean active = Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(jsonObject, "active"));
203                     oleStatisticalSearchingCodeBo.setActive(active);
204                     validObject = true;
205                 }catch(Exception e){
206                     e.printStackTrace();
207                     return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.INVALID_BOOLEAN, OLELoaderConstants.OLEloaderStatus.INVALID_BOOLEAN);
208                 }
209 
210             }
211 
212             if(jsonObject.has("sourceDate")){
213                 String sourceDate = getOleLoaderService().getStringValueFromJsonObject(jsonObject,"sourceDate");
214                 if(StringUtils.isNotBlank(sourceDate)){
215                     try{
216                         Date date = OLELoaderConstants.DATE_FORMAT.parse(sourceDate);
217                         oleStatisticalSearchingCodeBo.setSourceDate(new java.sql.Date(date.getTime()));
218                         validObject = true;
219                     }catch(Exception e){
220                         e.printStackTrace();
221                         return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.INVALID_DATE, OLELoaderConstants.OLEloaderStatus.INVALID_DATE);
222                     }
223                 }
224             }
225             if (oleStatisticalSearchingCodeBo != null && validObject) {
226                 OleStatisticalSearchingCodes oleStatisticalSearchingCode = getOleStatisticalSearchingCodeLoaderHelperService().getStatisticalSearchingCodeById(statisticalSearchingCodeId);
227                 if (oleStatisticalSearchingCode != null) {
228                     return getOleStatisticalSearchingCodeLoaderHelperService().updateOleStatisticalSearchingCode(oleStatisticalSearchingCode, oleStatisticalSearchingCodeBo, context);
229                 } else {
230                     return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.STAT_SEARCH_CODE_NOT_EXIST, OLELoaderConstants.OLEloaderStatus.STAT_SEARCH_CODE_NOT_EXIST);
231                 }
232             } else {
233                 return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.BAD_REQUEST, OLELoaderConstants.OLEloaderStatus.BAD_REQUEST);
234             }
235         }else {
236             return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.BAD_REQUEST, OLELoaderConstants.OLEloaderStatus.BAD_REQUEST);
237         }
238     }
239 
240     @Override
241     public Object exportStatisticalSearchingCodeById(String statisticalSearchingCodeId) {
242         LOG.info("Inside exportStatisticalSearchingCodeById method.");
243         OleStatisticalSearchingCodes oleStatisticalSearchingCode = getOleStatisticalSearchingCodeLoaderHelperService().getStatisticalSearchingCodeById(statisticalSearchingCodeId);
244         if(oleStatisticalSearchingCode != null){
245             return oleStatisticalSearchingCode;
246         }else{
247             return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.STAT_SEARCH_CODE_NOT_EXIST, OLELoaderConstants.OLEloaderStatus.STAT_SEARCH_CODE_NOT_EXIST);
248         }
249     }
250 
251     @Override
252     public Object exportStatisticalSearchingCodeByCode(String statisticalSearchingCodeCode) {
253         LOG.info("Inside exportStatisticalSearchingCodeByCode method.");
254         OleStatisticalSearchingCodes oleStatisticalSearchingCode = getOleStatisticalSearchingCodeLoaderHelperService().getStatisticalSearchingCodeByCode(statisticalSearchingCodeCode);
255         if(oleStatisticalSearchingCode != null){
256             return oleStatisticalSearchingCode;
257         }else{
258             return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.STAT_SEARCH_CODE_NOT_EXIST, OLELoaderConstants.OLEloaderStatus.STAT_SEARCH_CODE_NOT_EXIST);
259         }
260     }
261 
262     @Override
263     public List<OleStatisticalSearchingCodes> exportAllStatisticalSearchingCode() {
264         LOG.info("Inside exportAllStatisticalSearchingCode method.");
265         List<OleStatisticalSearchingCodes> oleStatisticalSearchingCode = getOleStatisticalSearchingCodeLoaderHelperService().getAllStatisticalSearchingCode();
266         return oleStatisticalSearchingCode;
267     }
268 }