View Javadoc
1   package org.kuali.ole.deliver.service;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.deliver.bo.OleCirculationDesk;
5   import org.kuali.ole.deliver.bo.OleCirculationDeskDetail;
6   import org.kuali.ole.deliver.form.OleCirculationDeskDetailForm;
7   import org.kuali.rice.kim.impl.identity.principal.PrincipalBo;
8   import org.kuali.rice.krad.service.BusinessObjectService;
9   import org.kuali.rice.krad.service.KRADServiceLocator;
10  
11  import java.util.ArrayList;
12  import java.util.HashMap;
13  import java.util.List;
14  import java.util.Map;
15  
16  
17  /**
18   * This is the service class for performing save and search operations on Circulation Desk Mapping
19   */
20  public class OleCirculationDeskDetailServiceImpl {
21  
22  
23      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleCirculationDeskDetailServiceImpl.class);
24      private BusinessObjectService businessObjectService;
25  
26      /**
27       * Gets the instance of BusinessObjectService
28       *
29       * @return businessObjectService(BusinessObjectService)
30       */
31      public BusinessObjectService getBusinessObjectService() {
32          if (null == businessObjectService) {
33              businessObjectService = KRADServiceLocator.getBusinessObjectService();
34          }
35          return businessObjectService;
36      }
37  
38  
39      /**
40       * This method is for populating the circulation details based on the available circulation desk
41       *
42       * @return oleCirculationDeskDetailList
43       */
44      public List<OleCirculationDeskDetail> populateCreateList() {
45          List<OleCirculationDeskDetail> oleCirculationDeskDetailList = new ArrayList<OleCirculationDeskDetail>();
46          List<OleCirculationDesk> oleCirculationDeskList = (List<OleCirculationDesk>) getBusinessObjectService().findAll(OleCirculationDesk.class);
47          OleCirculationDeskDetail oleCirculationDeskDetail;
48          for (int i = 0; i < oleCirculationDeskList.size(); i++) {
49              if (oleCirculationDeskList.get(i).isActive()) {
50                  oleCirculationDeskDetail = new OleCirculationDeskDetail();
51                  oleCirculationDeskDetail.setOleCirculationDesk(oleCirculationDeskList.get(i));
52                  oleCirculationDeskDetail.setCirculationDeskId(oleCirculationDeskList.get(i).getCirculationDeskId());
53                  oleCirculationDeskDetailList.add(oleCirculationDeskDetail);
54              }
55          }
56          return oleCirculationDeskDetailList;
57      }
58  
59      /**
60       * This method is for saving the circulation desk details for the operator
61       *
62       * @param oleCirculationDeskDetailForm
63       * @return oleCirculationDeskDetailForm
64       */
65      public OleCirculationDeskDetailForm performSave(OleCirculationDeskDetailForm oleCirculationDeskDetailForm) {
66  
67          oleCirculationDeskDetailForm.setMessage(null);
68  
69          String operatorId = oleCirculationDeskDetailForm.getOperatorId();
70          Map<String, String> opt = new HashMap<String, String>();
71          opt.put("principalId", operatorId);
72          List<PrincipalBo> matchedPrincipallList = (List<PrincipalBo>) getBusinessObjectService().findMatching(PrincipalBo.class, opt);
73  
74          if (matchedPrincipallList.size() == 0) {
75              StringBuffer selectBuffer = new StringBuffer(OLEConstants.CRCL_DSK_INVALID_OPR);
76              oleCirculationDeskDetailForm.setMessage(selectBuffer.toString());
77              LOG.error(selectBuffer);
78              return oleCirculationDeskDetailForm;
79          } else {
80              List<OleCirculationDeskDetail> oleCirculationDeskDetails = oleCirculationDeskDetailForm.getOleCirculationDetailsCreateList();
81              int count = 0;
82              for (int k = 0; k < oleCirculationDeskDetails.size(); k++) {
83                  if (oleCirculationDeskDetails.get(k).isDefaultLocation()) {
84                      count++;
85                  }
86              }
87              if (count == 1 && !oleCirculationDeskDetailForm.getOperatorId().isEmpty()) {
88                  List<OleCirculationDeskDetail> newOleCirculationDeskDetails = new ArrayList<OleCirculationDeskDetail>();
89  
90                  for (int i = 0; i < oleCirculationDeskDetails.size(); i++) {
91                      if (!oleCirculationDeskDetails.get(i).isDefaultLocation() && !oleCirculationDeskDetails.get(i).isAllowedLocation()) {
92                          getBusinessObjectService().delete(oleCirculationDeskDetails.get(i));
93                      } else {
94                          oleCirculationDeskDetails.get(i).setOperatorId(oleCirculationDeskDetailForm.getOperatorId());
95                          newOleCirculationDeskDetails.add(oleCirculationDeskDetails.get(i));
96                      }
97                  }
98  
99                  getBusinessObjectService().save(newOleCirculationDeskDetails);
100                 //Success message set for circulation desk created
101                 StringBuffer newDeskDetailForm = new StringBuffer(OLEConstants.CRCL_DSK_SUCCESS);
102                 oleCirculationDeskDetailForm.setMessage(newDeskDetailForm.toString());
103             } else if (oleCirculationDeskDetailForm.getOperatorId().isEmpty()) {
104                 StringBuffer selectBuffer = new StringBuffer(OLEConstants.CRCL_DSK_LOC_ERR);
105                 oleCirculationDeskDetailForm.setMessage(selectBuffer.toString());
106                 LOG.error(selectBuffer);
107                 return oleCirculationDeskDetailForm;
108             } else {
109                 if (count == 0) {
110                     StringBuffer defaultBuffer = new StringBuffer(OLEConstants.CRCL_DSK_NO_LOC_ERR);
111                     oleCirculationDeskDetailForm.setMessage(defaultBuffer.toString());
112                     LOG.error(defaultBuffer);
113                     return oleCirculationDeskDetailForm;
114                 }
115             }
116             oleCirculationDeskDetailForm.setOleCirculationDetailsCreateList(populateCreateList());
117             oleCirculationDeskDetailForm.setOperatorId(null);
118             return oleCirculationDeskDetailForm;
119         }
120     }
121 
122 
123     /**
124      * This method is for searching the circulation details
125      *
126      * @param oleCirculationDeskDetailForm
127      * @return oleCirculationDeskDetailForm
128      */
129     public OleCirculationDeskDetailForm performSearch(OleCirculationDeskDetailForm oleCirculationDeskDetailForm) {
130 
131         oleCirculationDeskDetailForm.setMessage(null);
132         String operatorId = oleCirculationDeskDetailForm.getOperatorId();
133 
134         Map<String, String> opt = new HashMap<String, String>();
135         opt.put("principalId", operatorId);
136         List<PrincipalBo> matchedPrincipallList = (List<PrincipalBo>) getBusinessObjectService().findMatching(PrincipalBo.class, opt);
137         if (matchedPrincipallList.size() == 0) {
138             StringBuffer selectBuffer = new StringBuffer(OLEConstants.CRCL_DSK_INVALID_OPR);
139             oleCirculationDeskDetailForm.setMessage(selectBuffer.toString());
140             LOG.error(selectBuffer);
141             return oleCirculationDeskDetailForm;
142         }
143 
144         if (operatorId.isEmpty()) {
145             StringBuffer selectBuffer = new StringBuffer(OLEConstants.CRCL_DSK_LOC_ERR);
146             oleCirculationDeskDetailForm.setMessage(selectBuffer.toString());
147             LOG.error(selectBuffer);
148             return oleCirculationDeskDetailForm;
149         } else {
150             Map<String, String> detailMap = new HashMap<String, String>();
151             detailMap.put("operatorId", oleCirculationDeskDetailForm.getOperatorId());
152             List<OleCirculationDeskDetail> searchedCirculationDeskDetailList = (List<OleCirculationDeskDetail>) getBusinessObjectService().findMatching(OleCirculationDeskDetail.class, detailMap);
153             List<OleCirculationDeskDetail> allCirculationDeskDetailList = populateCreateList();
154             List<OleCirculationDeskDetail> mergedList = new ArrayList<OleCirculationDeskDetail>();
155             mergedList.addAll(searchedCirculationDeskDetailList);
156             for (int i = 0; i < searchedCirculationDeskDetailList.size(); i++) {
157                 for (int j = 0; j < allCirculationDeskDetailList.size(); j++) {
158                     if (searchedCirculationDeskDetailList.get(i).getCirculationDeskId().equals(allCirculationDeskDetailList.get(j).getCirculationDeskId())) {
159                         allCirculationDeskDetailList.remove(j);
160                     }
161                 }
162             }
163             mergedList.addAll(allCirculationDeskDetailList);
164             if (mergedList != null && mergedList.size() > 0) {
165                 for (int i = 0; i < mergedList.size(); i++) {
166                     if (mergedList.get(i).getOleCirculationDesk() != null && !mergedList.get(i).getOleCirculationDesk().isActive()) {
167                         mergedList.remove(i);
168                     }
169                 }
170             }
171             oleCirculationDeskDetailForm.setOleCirculationDetailsCreateList(mergedList);
172             return oleCirculationDeskDetailForm;
173         }
174     }
175 
176 }
177