View Javadoc
1   package org.kuali.ole.deliver.service;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.apache.commons.lang.StringUtils;
5   import org.apache.log4j.Logger;
6   import org.kuali.ole.OLEConstants;
7   import org.kuali.ole.deliver.bo.OleCirculationDesk;
8   import org.kuali.ole.deliver.bo.OleCirculationDeskLocation;
9   import org.kuali.ole.describe.bo.OleLocation;
10  import org.kuali.ole.describe.bo.OleLocationLevel;
11  import org.kuali.ole.docstore.common.document.content.instance.LocationLevel;
12  import org.kuali.ole.service.OLEEResourceSearchService;
13  import org.kuali.rice.krad.service.BusinessObjectService;
14  import org.kuali.rice.krad.service.KRADServiceLocator;
15  
16  import java.util.*;
17  
18  /**
19   * Created by premkb on 4/8/15.
20   */
21  public class CircDeskLocationResolver {
22  
23      private static final Logger LOG = Logger.getLogger(CircDeskLocationResolver.class);
24  
25      private BusinessObjectService businessObjectService;
26      private List<String> locationLevelIds = new ArrayList<>();
27  
28      public void setLocationLevelIds(List<String> locationLevelIds) {
29          this.locationLevelIds = locationLevelIds;
30      }
31  
32  
33      public String getReplyToEmail(String itemLocation) {
34          OleCirculationDesk oleCirculationDesk = getCirculationDesk(itemLocation);
35          if (oleCirculationDesk != null && StringUtils.isNotBlank(oleCirculationDesk.getReplyToEmail())) {
36              return oleCirculationDesk.getReplyToEmail();
37          }
38          return null;
39      }
40  
41  
42      public OleCirculationDesk getCirculationDesk(String itemLocation) {
43          OleLocation oleLocation = null;
44          try {
45              if (StringUtils.isNotBlank(itemLocation)) {
46                  oleLocation = getLocationByLocationCode(itemLocation);
47              }
48          } catch (Exception e) {
49              LOG.error("Exception " + e);
50          }
51          if (oleLocation != null) {
52              OleCirculationDesk oleCirculationDesk = getCirculationDeskByLocationId(oleLocation.getLocationId());
53              return oleCirculationDesk;
54          }
55          return null;
56      }
57  
58  
59  
60      /**
61       * This method returns location using location code.
62       *
63       * @param locationCode
64       * @return
65       * @throws Exception
66       */
67      public OleLocation getLocationByLocationCode(String locationCode) throws Exception {
68          LOG.debug("Inside the getLocationByLocationCode method");
69          Map barMap = new HashMap();
70          barMap.put(OLEConstants.LOC_CD, locationCode);
71          List<OleLocation> matchingLocation = (List<OleLocation>) getBusinessObjectService().findMatching(OleLocation.class, barMap);
72          return matchingLocation != null && matchingLocation.size() > 0 ? matchingLocation.get(0) : null;
73      }
74  
75      public String getFullPathLocation(OleLocation oleLocation,List<String> locationLevelNames) throws Exception {
76          StringBuilder locationBuilder = new StringBuilder();
77          for(String locationLevelName : locationLevelNames){
78              String locationValue  = getLocationLevelName(oleLocation, locationLevelName);
79              if(StringUtils.isNotBlank(locationValue))
80                  locationBuilder.append( locationValue + OLEConstants.SLASH);
81          }
82          String fullLocation = locationBuilder.toString();
83          fullLocation = fullLocation.substring(0, fullLocation.length() - 1);
84          return fullLocation;
85      }
86  
87      private String getLocationLevelName(OleLocation oleLocation, String level) {
88          if (oleLocation == null || org.apache.commons.lang.StringUtils.isEmpty(oleLocation.getLevelId()))
89              return null;
90          if (oleLocation.getLevelId().equalsIgnoreCase(level))
91              return oleLocation.getLocationCode();
92          return getLocationLevelName(oleLocation.getOleLocation(), level);
93      }
94  
95      public String getFullPathLocationByName(OleLocation oleLocation) throws Exception {
96          String levelFullCode = "";
97          if (oleLocation != null) {
98              levelFullCode = oleLocation.getLocationName();
99              boolean parentId = oleLocation.getParentLocationId() != null ? true : false;
100             while (parentId) {
101                 OleLocation location = getLocationByParentLocationId(oleLocation.getParentLocationId());
102                 if (levelFullCode != null) {
103                     levelFullCode = location.getLocationName() + "-" + levelFullCode;
104                 }
105                 parentId = location.getParentLocationId() != null ? true : false;
106                 oleLocation = location;
107             }
108         }
109         return levelFullCode;
110     }
111 
112     private OleLocation getLocationByParentLocationId(String locationId){
113         Map criteriaMap = new HashMap();
114         criteriaMap.put(OLEConstants.LOCATION_ID, locationId);
115         OleLocation location = businessObjectService.findByPrimaryKey(OleLocation.class,
116                 criteriaMap);
117         return location;
118     }
119 
120     public OleLocation getLocationByParentIdAndLocationCode(String parentLocationId,String locationCode) throws Exception {
121         Map<String, String> map = new HashMap<String, String>();
122         map.put("parentLocationId", parentLocationId);
123         map.put("locationCode", locationCode);
124         List<OleLocation> matchingLocation = (List<OleLocation>) getBusinessObjectService().findMatching(OleLocation.class, map);
125         return matchingLocation != null && matchingLocation.size() > 0 ? matchingLocation.get(0) : null;
126     }
127 
128     public String circulationDeskLocations(OleCirculationDesk oleCirculationDesk) throws Exception {
129         String operatorsCirculationLocation = "";
130         if (oleCirculationDesk != null) {
131             if (oleCirculationDesk.getOleCirculationDeskLocations() != null) {
132                 StringBuffer location = new StringBuffer();
133                 for (OleCirculationDeskLocation oleCirculationDeskLocation : oleCirculationDesk.getOleCirculationDeskLocations()) {
134                     if(oleCirculationDeskLocation.getCirculationPickUpDeskLocation()==null || (oleCirculationDeskLocation.getCirculationPickUpDeskLocation()!=null && oleCirculationDeskLocation.getCirculationPickUpDeskLocation().trim().isEmpty())){
135                         location.append(getFullPathLocation(oleCirculationDeskLocation.getLocation(), getLocationLevelIds())).append(OLEConstants.DELIMITER_HASH);
136                     }
137                 }
138                 operatorsCirculationLocation = location.toString();
139                 operatorsCirculationLocation = operatorsCirculationLocation.replaceAll(OLEConstants.SLASH + OLEConstants.DELIMITER_HASH, OLEConstants.DELIMITER_HASH);
140             } else {
141                 throw new Exception(OLEConstants.NO_LOC_CIR_DESK);
142             }
143         }
144         return operatorsCirculationLocation;
145     }
146 
147     public OleCirculationDesk getCirculationDeskByLocationId(String locationId) {
148         OleCirculationDeskLocation oleCirculationDeskLocation = getOleCirculationDeskLocationByLocationId(locationId);
149         if (oleCirculationDeskLocation != null) {
150             return getOleCirculationDesk(oleCirculationDeskLocation.getCirculationDeskId());
151            /* Map<String, String> userMap = new HashMap<String, String>();
152             userMap.put("circulationDeskId", oleCirculationDeskLocation.getCirculationDeskId());
153             List<OleCirculationDesk> oleCirculationDesks = (List<OleCirculationDesk>) getBusinessObjectService().findMatching(OleCirculationDesk.class, userMap);
154             return oleCirculationDesks != null && oleCirculationDesks.size() > 0 ? oleCirculationDesks.get(0) : null;*/
155         }
156         return null;
157     }
158 
159     public OleCirculationDesk getOleCirculationDesk(String id) {
160         LOG.debug("Inside the getOleCirculationDesk method");
161         OleCirculationDesk oleCirculationDesk = getBusinessObjectService().findBySinglePrimaryKey(OleCirculationDesk.class, id);
162         return oleCirculationDesk;
163     }
164 
165     public OleCirculationDeskLocation getOleCirculationDeskLocationByLocationId(String locationId) {
166         Map<String, String> locationMap = new HashMap<String, String>();
167         locationMap.put("circulationDeskLocation", locationId);
168         List<OleCirculationDeskLocation> oleCirculationDeskLocations = (List<OleCirculationDeskLocation>) getBusinessObjectService().findMatching(OleCirculationDeskLocation.class, locationMap);
169         return oleCirculationDeskLocations != null && oleCirculationDeskLocations.size() > 0 ? oleCirculationDeskLocations.get(0) : null;
170     }
171 
172     public OleCirculationDesk getCirculationDeskByLocationCode(String locationCode) {
173         Map<String, String> userMap = new HashMap<String, String>();
174         userMap.put(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_CD, locationCode);
175         List<OleCirculationDesk> oleCirculationDesks = (List<OleCirculationDesk>) getBusinessObjectService().findMatching(OleCirculationDesk.class, userMap);
176         return oleCirculationDesks != null && oleCirculationDesks.size() > 0 ? oleCirculationDesks.get(0) : null;
177     }
178 
179 
180     /**
181      * * This method returns location level using location name.
182      *
183      * @param levelName
184      * @return List
185      * @throws Exception
186      */
187     public OleLocationLevel getLocationLevelByName(String levelName) throws Exception {
188         LOG.debug("Inside the getLocationLevelByName method");
189         Map barMap = new HashMap();
190         barMap.put(OLEConstants.LEVEL_NAME, levelName);
191         List<OleLocationLevel> matchingLocationLevel = (List<OleLocationLevel>) getBusinessObjectService().findMatching(OleLocationLevel.class, barMap);
192         return matchingLocationLevel.get(0);
193     }
194 
195     public List<String> getLocationLevelIds() {
196         if(CollectionUtils.isEmpty(this.locationLevelIds)){
197             List<OleLocationLevel> locationLevel = (List<OleLocationLevel>) KRADServiceLocator.getBusinessObjectService().findAllOrderBy(OleLocationLevel.class, OLEConstants.LEVEL_ID, true);
198             for (OleLocationLevel oleLocationLevel : locationLevel) {
199                 this.locationLevelIds.add(oleLocationLevel.getLevelId());
200             }
201         }
202         return this.locationLevelIds;
203     }
204 
205     public Map<String, String> getLocationMap(String itemLocation){
206         Map<String, String> locationMap = new HashMap<String, String>();
207         String[] locationArray = itemLocation.split("['/']");
208         List<String> locationList = Arrays.asList(locationArray);
209         for (String value : locationList) {
210             Map<String, String> requestMap = new HashMap<>();
211             /*requestMap.put(OLEConstants.LOCATION_CODE, value);
212             List<OleLocation> oleLocations = (List<OleLocation>) getBusinessObjectService().findMatching(OleLocation.class, requestMap);*/
213             OleLocation oleLocations = null;
214             try {
215                  oleLocations = getLocationByLocationCode(value);
216             }catch (Exception location){
217                 LOG.error("Location Code does not exist." + location.getMessage());
218             }
219             if (oleLocations != null) {
220                 String locationLevelId = oleLocations.getLevelId();
221                // requestMap.clear();
222                 requestMap.put(OLEConstants.LEVEL_ID, locationLevelId);
223                 List<OleLocationLevel> oleLocationLevels = (List<OleLocationLevel>) getBusinessObjectService().findMatching(OleLocationLevel.class, requestMap);
224                 if (oleLocationLevels != null && oleLocationLevels.size() > 0) {
225                     OleLocationLevel oleLocationLevel = new OleLocationLevel();
226                     oleLocationLevel = oleLocationLevels.get(0);
227                     if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_CAMPUS)) {
228                         locationMap.put(OLEConstants.ITEM_CAMPUS, value);
229                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_INSTITUTION)) {
230                         locationMap.put(OLEConstants.ITEM_INSTITUTION, value);
231                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_COLLECTION)) {
232                         locationMap.put(OLEConstants.ITEM_COLLECTION, value);
233                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_LIBRARY)) {
234                         locationMap.put(OLEConstants.ITEM_LIBRARY, value);
235                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_SHELVING)) {
236                         locationMap.put(OLEConstants.ITEM_SHELVING, value);
237                     }
238                 }
239             }
240         }
241         return locationMap;
242     }
243 
244     public LocationLevel createLocationLevel(String locationName, LocationLevel locationLevel) {
245         LOG.debug("Inside the createLocationLevel method");
246         if (locationName != null && !locationName.equalsIgnoreCase("")) {
247             BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
248             String[] names = locationName.split("/");
249             Map parentCriteria = new HashMap();
250             parentCriteria.put(OLEConstants.LOC_CD, names[0]);
251             OleLocation oleLocationCollection = businessObjectService.findByPrimaryKey(OleLocation.class, parentCriteria);
252             String locationCode = oleLocationCollection.getLocationCode();
253             String levelCode = oleLocationCollection.getOleLocationLevel().getLevelName();
254             locationLevel.setName(locationCode);
255             locationLevel.setLevel(levelCode);
256             String locName = "";
257             if (locationName.contains(OLEConstants.SLASH))
258                 locName = locationName.replace(names[0] + OLEConstants.SLASH, "");
259             else
260                 locName = locationName.replace(names[0], "");
261             if (locName != null && !locName.equals("")) {
262                 LocationLevel newLocationLevel = new LocationLevel();
263                 locationLevel.setLocationLevel(createLocationLevel(locName, newLocationLevel));
264             }
265         }
266         return locationLevel;
267     }
268 
269     public BusinessObjectService getBusinessObjectService() {
270         if (null == businessObjectService) {
271             businessObjectService = KRADServiceLocator.getBusinessObjectService();
272         }
273         return businessObjectService;
274     }
275 }