View Javadoc
1   package org.kuali.ole.describe.bo;
2   
3   import org.kuali.ole.describe.form.WorkEInstanceOlemlForm;
4   import org.kuali.ole.docstore.common.document.content.instance.Location;
5   import org.kuali.ole.docstore.common.document.content.instance.LocationLevel;
6   import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
7   import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
8   import org.kuali.rice.krad.service.BusinessObjectService;
9   import org.kuali.rice.krad.service.KRADServiceLocator;
10  
11  import java.util.HashMap;
12  import java.util.Map;
13  
14  /**
15   * Created with IntelliJ IDEA.
16   * User: sambasivam
17   * Date: 9/6/13
18   * Time: 7:24 PM
19   * To change this template use File | Settings | File Templates.
20   */
21  public class EInstanceFormDataHandler {
22      private HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
23      public void buildLocationLevels(WorkEInstanceOlemlForm workEInstanceOlemlForm) {
24          OleHoldings eHoldings = workEInstanceOlemlForm.getSelectedEHoldings();
25          Location location = new Location();
26          LocationLevel locationLevel = new LocationLevel();
27  
28          String holdingsLocationName = eHoldings.getLocation().getLocationLevel().getName();
29          if (!holdingsLocationName.equalsIgnoreCase("")) {
30              locationLevel = createLocationLevel(holdingsLocationName, locationLevel);
31              location.setLocationLevel(locationLevel);
32              location.setPrimary("true");
33              location.setStatus("permanent");
34              eHoldings.setLocation(location);
35          } else {
36              eHoldings.setLocation(null);
37          }
38  
39      }
40  
41      public LocationLevel createLocationLevel(String locationName, LocationLevel locationLevel) {
42          if (locationName != null && !locationName.equalsIgnoreCase("")) {
43              BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
44              String[] names = locationName.split("/");
45              Map parentCriteria = new HashMap();
46              parentCriteria.put("locationCode", names[0]);
47              OleLocation oleLocationCollection = businessObjectService.findByPrimaryKey(OleLocation.class, parentCriteria);
48              String locationCode = oleLocationCollection.getLocationCode();
49              String levelCode = oleLocationCollection.getOleLocationLevel().getLevelName();
50              locationLevel.setName(locationCode);
51              locationLevel.setLevel(levelCode);
52              String locName = "";
53              if (locationName.contains("/"))
54                  locName = locationName.replace(names[0] + "/", "");
55              else
56                  locName = locationName.replace(names[0], "");
57              if (locName != null && !locName.equals("")) {
58                  LocationLevel newLocationLevel = new LocationLevel();
59                  locationLevel.setLocationLevel(createLocationLevel(locName, newLocationLevel));
60              }
61          }
62          return locationLevel;
63      }
64  
65      public String buildEInstanceRecordForDocStore(WorkEInstanceOlemlForm workEInstanceOlemlForm) {
66  
67          String content = holdingOlemlRecordProcessor.toXML(workEInstanceOlemlForm.getSelectedEHoldings());
68          return content;
69      }
70  
71  
72  
73      public void setLocationDetails(WorkEInstanceOlemlForm workEInstanceOlemlForm) {
74          OleHoldings eHoldings = workEInstanceOlemlForm.getSelectedEHoldings();
75          if (eHoldings != null) {
76              Location oleHoldingsLocation = eHoldings.getLocation();
77              if (oleHoldingsLocation != null) {
78                  LocationLevel holdingsLocationLevel = oleHoldingsLocation.getLocationLevel();
79                  String holdingLocationCode = getLocationCode(holdingsLocationLevel);
80                  if (holdingsLocationLevel != null) {
81                      eHoldings.getLocation().getLocationLevel().setName(holdingLocationCode);
82                  }
83  
84              }
85          }
86  
87      }
88  
89      private String getLocationCode(LocationLevel locationLevel) {
90          String locationCode = "";
91          while (locationLevel != null) {
92              String name = locationLevel.getName();
93              if (name != null) {
94                  BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
95                  Map parentCriteria = new HashMap();
96                  parentCriteria.put("locationCode", name);
97                  OleLocation oleLocationCollection = businessObjectService.findByPrimaryKey(OleLocation.class, parentCriteria);
98                  if (oleLocationCollection != null) {
99                      String code = oleLocationCollection.getLocationCode();
100                     if (locationCode.equalsIgnoreCase("")) {
101                         locationCode = code;
102                     } else {
103                         locationCode = locationCode + "/" + code;
104                     }
105                 }
106             }
107             locationLevel = locationLevel.getLocationLevel();
108         }
109         return locationCode;
110     }
111 
112 }