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.OleItemAvailableStatus;
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.OLEItemAvailableStatusBo;
16  import org.kuali.ole.loaders.describe.service.OLEItemAvailableStatusLoaderHelperService;
17  import org.kuali.ole.loaders.describe.service.OLEItemAvailableStatusLoaderService;
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 OLEItemAvailableStatusLoaderServiceImpl implements OLEItemAvailableStatusLoaderService {
29  
30      private static final Logger LOG = Logger.getLogger(OLEItemAvailableStatusLoaderServiceImpl.class);
31      private OLEItemAvailableStatusLoaderHelperService oleItemAvailableStatusLoaderHelperService;
32      private OLELoaderService oleLoaderService;
33      private BusinessObjectService businessObjectService;
34  
35      public OLEItemAvailableStatusLoaderHelperService getOleItemAvailableStatusLoaderHelperService() {
36          if(oleItemAvailableStatusLoaderHelperService == null){
37              oleItemAvailableStatusLoaderHelperService = new OLEItemAvailableStatusLoaderHelperServiceImpl();
38          }
39          return oleItemAvailableStatusLoaderHelperService;
40      }
41  
42      public void setOleItemAvailableStatusLoaderHelperService(OLEItemAvailableStatusLoaderHelperService oleItemAvailableStatusLoaderHelperService) {
43          this.oleItemAvailableStatusLoaderHelperService = oleItemAvailableStatusLoaderHelperService;
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 importItemAvailableStatus(String bodyContent, HttpContext context) {
70          LOG.info("Inside importItemAvailableStatus method.");
71          OLELoaderImportResponseBo oleLoaderImportResponseBo = new OLELoaderImportResponseBo();
72          List<Integer> rejectItemAvailableStatusList = new ArrayList<Integer>();
73          List<JSONObject> createdItemAvailableStatusObject = 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 itemAvailableStatusJsonArray = getOleLoaderService().getJsonArrayFromString(items);
81                      for (int index = 0; index < itemAvailableStatusJsonArray.length(); index ++) {
82                          JSONObject jsonObject = null;
83                          OleItemAvailableStatus itemAvailableStatus = new OleItemAvailableStatus();
84                          try {
85                              jsonObject = (JSONObject)itemAvailableStatusJsonArray.get(index);
86                              if(jsonObject != null){
87                                  if(jsonObject.has("name")){
88                                      String name = getOleLoaderService().getStringValueFromJsonObject(jsonObject,"name");
89                                      if(StringUtils.isNotBlank(name)){
90                                          itemAvailableStatus.setItemAvailableStatusName(name);
91                                          validObject = true;
92                                      }
93                                  }
94                                  if(jsonObject.has("code")){
95                                      String code = getOleLoaderService().getStringValueFromJsonObject(jsonObject,"code");
96                                      if(StringUtils.isNotBlank(code)){
97                                          itemAvailableStatus.setItemAvailableStatusCode(code);
98                                          validObject = true;
99                                      }
100                                 }
101 
102                                 if(jsonObject.has("active")){
103                                     try{
104                                         boolean active = Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(jsonObject, "active"));
105                                         itemAvailableStatus.setActive(active);
106                                         validObject = true;
107                                     }catch(Exception e){
108                                         e.printStackTrace();
109                                         rejectItemAvailableStatusList.add(index+1);
110                                         continue;
111                                     }
112 
113                                 }
114 
115                             }
116                             if(itemAvailableStatus != null && validObject){
117                                 if(getOleItemAvailableStatusLoaderHelperService().getItemAvailableStatusByCode(itemAvailableStatus.getItemAvailableStatusCode()) == null){
118                                     try {
119                                         itemAvailableStatus = getBusinessObjectService().save(itemAvailableStatus);
120                                         createdItemAvailableStatusObject.add((JSONObject)getOleItemAvailableStatusLoaderHelperService().formItemAvailableStatusExportResponse(itemAvailableStatus, OLELoaderConstants.OLELoaderContext.ITEM_TYPE,
121                                                 context.getRequest().getRequestUri().toASCIIString(), false));
122                                     } catch (Exception e) {
123                                         rejectItemAvailableStatusList.add(index+1);
124                                         continue;
125                                     }
126                                 }else{
127                                     rejectItemAvailableStatusList.add(index+1);
128                                     continue;
129                                 }
130 
131 
132                             }else{
133                                 rejectItemAvailableStatusList.add(index+1);
134                                 continue;
135                             }
136                         } catch (JSONException e) {
137                             e.printStackTrace();
138                             rejectItemAvailableStatusList.add(index+1);
139                             continue;
140                         }
141                     }
142                 }else{
143                     return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.BAD_REQUEST, OLELoaderConstants.OLEloaderStatus.BAD_REQUEST);
144                 }
145 
146             }
147         }else{
148             return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.BAD_REQUEST, OLELoaderConstants.OLEloaderStatus.BAD_REQUEST);
149         }
150         oleLoaderImportResponseBo.setOleRejectedBos(rejectItemAvailableStatusList);
151         oleLoaderImportResponseBo.setOleCreatedBos(createdItemAvailableStatusObject);
152         return oleLoaderImportResponseBo;
153     }
154 
155     @Override
156     public OLELoaderResponseBo updateItemAvailableStatusById(String itemAvailableStatusId, String bodyContent, HttpContext context) {
157         LOG.info("Inside updateItemAvailableStatusById method.");
158         OLEItemAvailableStatusBo oleItemAvailableStatusBo = new OLEItemAvailableStatusBo();
159         JSONObject jsonObject = getOleLoaderService().getJsonObjectFromString(bodyContent);
160         boolean validObject = false;
161         if (jsonObject != null) {
162             if (jsonObject.has("name")) {
163                 String name = getOleLoaderService().getStringValueFromJsonObject(jsonObject, "name");
164                 if (StringUtils.isNotBlank(name)) {
165                     oleItemAvailableStatusBo.setItemAvailableStatusName(name);
166                     validObject = true;
167                 }
168             }
169 
170             if(jsonObject.has("active")){
171                 try{
172                     boolean active = Boolean.parseBoolean(getOleLoaderService().getStringValueFromJsonObject(jsonObject, "active"));
173                     oleItemAvailableStatusBo.setActive(active);
174                     validObject = true;
175                 }catch(Exception e){
176                     e.printStackTrace();
177                     return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.INVALID_BOOLEAN, OLELoaderConstants.OLEloaderStatus.INVALID_BOOLEAN);
178                 }
179 
180             }
181 
182             if (oleItemAvailableStatusBo != null && validObject) {
183                 OleItemAvailableStatus oleItemAvailableStatus = getOleItemAvailableStatusLoaderHelperService().getItemAvailableStatusById(itemAvailableStatusId);
184                 if (oleItemAvailableStatus != null) {
185                     return getOleItemAvailableStatusLoaderHelperService().updateOleItemAvailableStatus(oleItemAvailableStatus,oleItemAvailableStatusBo,context);
186                 } else {
187                     return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.ITEM_STATUS_NOT_EXIST, OLELoaderConstants.OLEloaderStatus.ITEM_STATUS_NOT_EXIST);
188                 }
189             } else {
190                 return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.BAD_REQUEST, OLELoaderConstants.OLEloaderStatus.BAD_REQUEST);
191             }
192         }else {
193             return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.BAD_REQUEST, OLELoaderConstants.OLEloaderStatus.BAD_REQUEST);
194         }
195     }
196 
197     @Override
198     public Object exportItemAvailableStatusById(String itemAvailableStatusId) {
199         LOG.info("Inside exportItemAvailableStatusById method.");
200         OleItemAvailableStatus oleItemAvailableStatus = getOleItemAvailableStatusLoaderHelperService().getItemAvailableStatusById(itemAvailableStatusId);
201         if(oleItemAvailableStatus != null){
202             return oleItemAvailableStatus;
203         }else{
204             return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.ITEM_STATUS_NOT_EXIST, OLELoaderConstants.OLEloaderStatus.ITEM_STATUS_NOT_EXIST);
205         }
206     }
207 
208     @Override
209     public Object exportItemAvailableStatusByCode(String itemAvailableStatusCode) {
210         LOG.info("Inside exportItemAvailableStatusByCode method.");
211         OleItemAvailableStatus oleItemAvailableStatus = getOleItemAvailableStatusLoaderHelperService().getItemAvailableStatusByCode(itemAvailableStatusCode);
212         if(oleItemAvailableStatus != null){
213             return oleItemAvailableStatus;
214         }else{
215             return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.ITEM_STATUS_NOT_EXIST, OLELoaderConstants.OLEloaderStatus.ITEM_STATUS_NOT_EXIST);
216         }
217     }
218 
219     @Override
220     public List<OleItemAvailableStatus> exportAllItemAvailableStatus() {
221         LOG.info("Inside exportAllItemAvailableStatus method.");
222         List<OleItemAvailableStatus> oleItemAvailableStatus = getOleItemAvailableStatusLoaderHelperService().getAllItemAvailableStatus();
223         return oleItemAvailableStatus;
224     }
225 }