View Javadoc
1   package org.kuali.ole.describe.service.impl;
2   
3   
4   import org.apache.commons.lang.StringUtils;
5   import org.apache.log4j.Logger;
6   import org.kuali.ole.OLEConstants;
7   import org.kuali.ole.docstore.common.document.content.instance.Item;
8   import org.kuali.ole.docstore.common.document.content.instance.ExtentOfOwnership;
9   import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
10  import org.kuali.ole.describe.form.InstanceEditorForm;
11  import org.kuali.ole.describe.service.InstanceEditorTreeService;
12  
13  import java.util.*;
14  
15  /**
16   * InstanceEditorTreeServiceImpl is the service implementation class for Instance Editor  tree structure in left pane.
17   */
18  public class InstanceEditorTreeServiceImpl implements InstanceEditorTreeService {
19      private static final Logger LOG = Logger.getLogger(InstanceEditorTreeServiceImpl.class);
20  
21      /**
22       * This method creates TreeStructure (InnerHtML as a String and make treeStructure with jQuery) for instance with Holdings as parent node
23       * and items as child nodes grouped by formerShelvingLocation.
24       *
25       * @param instanceEditorForm
26       * @return String
27       */
28      @Override
29      public String createTreeStructure(InstanceEditorForm instanceEditorForm) {
30          String textualHolding = null;
31          List<ExtentOfOwnership> extentOfOwnership = instanceEditorForm.getInstance().getOleHoldings().getExtentOfOwnership();
32          if (extentOfOwnership != null && extentOfOwnership.size() > 0) {
33              textualHolding = extentOfOwnership.get(0).getTextualHoldings();
34              LOG.info("textualHolding-->" + textualHolding);
35          }
36          textualHolding = (textualHolding != null && !textualHolding.trim().equals("")) ?
37                  textualHolding : OLEConstants.NON_SERIAL_HOLDINGS_TEXT;
38          String holdingUuid = instanceEditorForm.getInstance().getOleHoldings().getHoldingsIdentifier();
39          String treeData = null;
40          String formerShelvingLocation = null;
41          if (instanceEditorForm != null) {
42              List<Item> oleItems = new ArrayList<Item>();
43              oleItems = instanceEditorForm.getInstance().getItems().getItem();
44              List<String> itemIdentifiersList = new ArrayList<String>();
45              for (int i = 0; i < oleItems.size(); i++) {
46                  itemIdentifiersList.add(oleItems.get(i).getItemIdentifier());
47                  oleItems.get(i).setItemIdentifier(oleItems.get(i).getItemIdentifier() + "_" + i);
48              }
49              Map<String, List<Item>> oleItemMap = getItemMap(instanceEditorForm);
50              treeData = "<ul id=\"navigation\">" +
51                      "<li><div id=\"rightClick\"><b>" + textualHolding + "</b></div>" +
52                      "<div class=\"vmenu\">\n" +
53                      "<div class=\"add_li\"><span>Add Item</span></div>\n" +
54                      "<div class=\"delete_li\"><span>Delete Instance</span></div></div>" +
55                      "<div id=\"itmMenu\" style=\"border: 1px solid #aaa; border-left:1px solid #ccc; padding: 10px; display: none; position: absolute; top: 663px; left: 539px; background:#fff;\">" +  //background-color: rgb(238, 238, 238);
56                      "<table>" +
57                      "<tr><td class=\"delete_itm\"width=\"100%\" >Delete Item</td></tr> " +   //style="border: 1px solid rgb(0, 0, 0);
58                      "</table>" +
59                      "</div>";
60  
61              Set set = oleItemMap.entrySet();
62              Iterator itr = set.iterator();
63              if (itr.hasNext()) {
64                  int itemSize = 1;
65                  while (itr.hasNext()) {
66  
67                      boolean isElectronic = false;
68                      boolean isItem = false;
69                      boolean isNoLocation = false;
70                      Map.Entry itemEntry = (Map.Entry) itr.next();
71                      String location = (String) itemEntry.getKey();
72                      List<Item> itemsEntry = (List<Item>) itemEntry.getValue();
73                      if (location.equalsIgnoreCase(OLEConstants.ITEM_ELECTRONIC)) {
74                          isElectronic = true;
75                      } else if (location.equalsIgnoreCase(OLEConstants.NO_LOCATION_ITEM)) {
76                          isNoLocation = true;
77                      } else if (location.equalsIgnoreCase(OLEConstants.ITEM_LEVEL_TEXT)) {
78                          isItem = true;
79                      } else if (!isElectronic && !isItem && !isNoLocation) {
80                          treeData = treeData +
81                                  "<ul>" +
82                                  "<li><label>" + location + "</label>" +
83                                  "<ul>";
84                      }
85                      for (int i = 0; i < itemsEntry.size(); i++) {
86                          String itemIdentifier = itemsEntry.get(i).getItemIdentifier();
87                          if (!isElectronic && !isItem && !isNoLocation) {
88                              OleHoldings oleHoldings = instanceEditorForm.getInstance().getOleHoldings();
89                              String itemLevelContent = buildNonElectronicItemContent(itemsEntry.get(i), oleHoldings);
90                              treeData = treeData + "<li><div class=\"itemIdentifierClass\" id=\"" + itemIdentifier + "\">" + itemLevelContent + "</div>" +
91                                      "</li>";
92                          } else if (isElectronic) {
93                              String itemLevelContent = itemsEntry.get(i).getAccessInformation().getUri().getValue();
94                              treeData = treeData +
95                                      "<ul>" +
96                                      "<li><div class=\"itemIdentifierClass\" id=\"" + itemIdentifier + "\">" + itemLevelContent + "</div>" +
97                                      "</li>" +
98                                      "</ul>";
99                          } else {
100                             treeData = treeData +
101                                     "<ul>" +
102                                     "<li><div class=\"itemIdentifierClass\" id=\"" + itemIdentifier + "\">" + OLEConstants.ITEM_LEVEL_TEXT + itemSize + "</div>" +
103                                     "</li>" +
104                                     "</ul>";
105                             itemSize++;
106                         }
107 
108                     }
109                     if (!isElectronic && !isItem && !isNoLocation) {
110                         treeData = treeData +
111                                 "</ul>" +
112                                 "</li>" +
113                                 "</ul>";
114                     }
115                 }
116             } else {
117                 for (int j = 0; j < oleItems.size(); j++) {
118                     String itemIdentifier = oleItems.get(j).getItemIdentifier();
119                     int itemSize = j + 1;
120                     treeData = treeData +
121                             "<ul>" +
122                             "<li><div class=\"itemIdentifierClass\" id=\"" + itemIdentifier + "\">" + OLEConstants.ITEM_LEVEL_TEXT + itemSize + "</div>" +
123                             "</li>" +
124                             "</ul>";
125                 }
126             }
127             treeData = treeData + "</li>" +
128                     "</ul>";
129 
130             for (int j = 0; j < oleItems.size(); j++) {
131                 oleItems.get(j).setItemIdentifier(itemIdentifiersList.get(j));
132             }
133         }
134 
135         LOG.info("tree data in createTreeStructure() method-->" + treeData);
136         return treeData;
137     }
138 
139     private Map<String, List<Item>> getItemMap(InstanceEditorForm instanceEditorForm) {
140         List<Item> listItems = null;
141         String itemLocation = null;
142         Map<String, List<Item>> oleItemMap = new HashMap<String, List<Item>>();
143         List<Item> oleItems;
144         oleItems = instanceEditorForm.getInstance().getItems().getItem();
145         String holdingsLocations = instanceEditorForm.getInstance().getOleHoldings().getLocation().getLocationLevel().getName();
146         for (Item oleItem : oleItems) {
147             if (oleItem.getLocation() != null) {
148                 itemLocation = oleItem.getLocation().getLocationLevel().getName();
149                 LOG.info("item location =" + itemLocation);
150                 addItemsToMap(itemLocation, oleItemMap, oleItem);
151                 LOG.info("map size for item location = " + oleItemMap.size());
152             } else if (!StringUtils.isBlank(holdingsLocations)) {
153                 addItemsToMap(holdingsLocations, oleItemMap, oleItem);
154                 LOG.info("map size for holdings location = " + oleItemMap.size());
155             }
156         }
157         return oleItemMap;
158     }
159 
160     private void addItemsToMap(String itemLocation, Map<String, List<Item>> oleItemMap, Item oleItem) {
161         List<Item> listItems;
162         if (oleItemMap.containsKey(itemLocation)) {
163             List<Item> itemList = oleItemMap.get(itemLocation);
164             itemList.add(oleItem);
165             oleItemMap.put(itemLocation, itemList);
166             LOG.info("item list size in if = " + itemList.size());
167         } else {
168             listItems = new ArrayList<Item>();
169             listItems.add(oleItem);
170             LOG.info("item list size in else = " + listItems.size());
171             oleItemMap.put(itemLocation, listItems);
172         }
173     }
174 
175     /**
176      * This method returns a Map with formerShelvingLocation as key and a List of Items belongs to this formerShelvingLocation as value.
177      *
178      * @param oleItems
179      * @return Map<String,List<OleItem>>
180      */
181     private Map<String, List<Item>> getItemMap(List<Item> oleItems) {
182 
183         Map<String, List<Item>> oleItemMap = new HashMap<String, List<Item>>();
184 
185         if (oleItems != null) {
186             for (Item item : oleItems) {
187                 String formerShelvingLocation = null;
188                 String uri = null;
189                 //boolean isElectronic = false;
190                 List<Item> listItems = new ArrayList<Item>();
191                 //List<PhysicalLocation> physicalLocations =  item.getLocation();
192                 /*if ((physicalLocations != null && physicalLocations.size() > 0)
193                         && physicalLocations.get(0).getFormerShelvingLocation() != null &&
194                         physicalLocations.get(0).getFormerShelvingLocation().size() > 0) {
195                     formerShelvingLocation = physicalLocations.get(0).getFormerShelvingLocation().get(0);
196                 }*/
197                 if (item.getLocation() != null) {
198                     formerShelvingLocation = item.getLocation().getLocationLevel().getName();
199                 }
200                 if (item.getAccessInformation() != null && item.getAccessInformation().getUri() != null) {
201                     uri = item.getAccessInformation().getUri().getValue();
202                 }
203                 /*if (item.getAccessInformation() != null) {
204                     isElectronic = (item.getAccessInformation().getUri() != null ? true:false);
205                 }*/
206                 if (formerShelvingLocation != null) {
207                     for (Item items : oleItems) {
208                         //if(items.getLocation().size()>0 && items.getLocation().get(0).getFormerShelvingLocation().size()>0){
209                         String itemLocation = null;
210                         if (items.getLocation() != null) {
211                             itemLocation = items.getLocation().getLocationLevel().getName();
212                         }
213                         if ((formerShelvingLocation != null && !formerShelvingLocation.trim().equals("")) && itemLocation != null) {
214                             if (formerShelvingLocation.equalsIgnoreCase(items.getLocation().getLocationLevel().getName())) {
215                                 listItems.add(items);
216                             }
217                         }
218                         /*else if(isElectronic && formerShelvingLocation == null) {
219                             formerShelvingLocation = OLEConstants.ITEM_ELECTRONIC;
220                             listItems.add(items);
221                         }*/
222                         else if (formerShelvingLocation.trim().equals("")) {
223                             //if(formerShelvingLocation.equalsIgnoreCase(items.getLocation())){
224                             listItems.add(items);
225                             //}
226                         }
227                         //}
228                     }
229                     if (formerShelvingLocation == null || formerShelvingLocation.trim().equals("")) {
230                         formerShelvingLocation = OLEConstants.ITEM_LEVEL_TEXT;
231                     }
232 
233                     oleItemMap.put(formerShelvingLocation, listItems);
234                 }
235 /*                else if(formerShelvingLocation == null && uri != null && !uri.equalsIgnoreCase("")) {
236                     for(Item items : oleItems){
237                         String uriVal = null;
238                         String itemLocation = null;
239                         if(items.getLocation()!=null){
240                             itemLocation = items.getLocation().getLocationLevel().getName();
241                         }
242                         if(items.getAccessInformation()!= null && items.getAccessInformation().getUri()!= null){
243                             uriVal = items.getAccessInformation().getUri().getValue();
244                         }
245                         if(itemLocation == null && uriVal != null && !uriVal.trim().equals("")){
246                             formerShelvingLocation = OLEConstants.ITEM_ELECTRONIC;
247                             listItems.add(items);
248                         }
249                         *//*else if(uriVal.trim().equals("")){
250                             //if(formerShelvingLocation.equalsIgnoreCase(items.getLocation())){
251                             listItems.add(items);
252                             //}
253                         }*//*
254                     }
255                     if(formerShelvingLocation == null || formerShelvingLocation.trim().equals("")) {
256                         formerShelvingLocation = OLEConstants.ITEM_LEVEL_TEXT;
257                     }
258                     oleItemMap.put(formerShelvingLocation,listItems);
259                 }*/
260                 /*else {
261                     for(Item items : oleItems){
262                         if((formerShelvingLocation == null && uri == null) || uri.trim().equals("")) {
263                             *//*String uriVal = null;
264                             String itemLocation = null;
265                             if(items.getLocation()!=null){
266                                 itemLocation = items.getLocation().getLocationLevel().getName();
267                             }
268                             if(items.getAccessInformation()!= null && items.getAccessInformation().getUri()!= null){
269                                 uriVal = items.getAccessInformation().getUri().getValue();
270                             }*//*
271                             if(formerShelvingLocation == null && uri==null){
272                                 listItems.add(items);
273                             }
274                         }
275                     }
276                     if(listItems.size()>0){
277                         String emptyFormerShelvingLocation = OLEConstants.NO_LOCATION_ITEM;
278                         oleItemMap.put(emptyFormerShelvingLocation,listItems);
279                     }
280                 }*/
281 /*                else {
282                     for(Item items : oleItems){
283                         if(formerShelvingLocation == null) {
284                             listItems.add(items);
285                         }
286                     }
287                     if(listItems.size()>0){
288                         String emptyFormerShelvingLocation = OLEConstants.NO_LOCATION_ITEM;
289                         oleItemMap.put(emptyFormerShelvingLocation,listItems);
290                     }
291                 }*/
292 
293             }
294         }
295         return oleItemMap;
296     }
297 
298 
299     /**
300      * Method to build Non_Electronic Item record node content
301      *
302      * @param oleItem
303      * @return nonElectronicItemContent
304      */
305     private String buildNonElectronicItemContent(Item oleItem, OleHoldings oleHoldings) {
306 
307         String callNumberPrefix = null;
308         String copyNumber = null;
309         String copyNumberLabel = null;
310         String barcode = null;
311         String status = null;
312         if (oleItem.getCallNumber() != null) {
313             callNumberPrefix = (oleItem.getCallNumber().getPrefix());
314         } else if (oleHoldings.getCallNumber() != null) {
315             callNumberPrefix = (oleHoldings.getCallNumber().getPrefix());
316         }
317         String enumeration = oleItem.getEnumeration();
318         String chronology = oleItem.getChronology();
319         String textToPrefix = OLEConstants.NON_ELECTRONIC_PREFIX_TEXT;
320         copyNumber = oleItem.getCopyNumber();
321         copyNumberLabel = oleItem.getCopyNumberLabel();
322         if (oleItem.getAccessInformation() != null) {
323             barcode = oleItem.getAccessInformation().getBarcode();
324         }
325         status = oleItem.getItemStatus().getCodeValue();
326         String nonElectronicItemContent = new StringBuffer().append(null == copyNumberLabel ? "Empty" : copyNumberLabel).append(" ")
327                 .append(null == copyNumber ? "Empty" : copyNumber).append(" ")
328                 .append(" ").append(null == callNumberPrefix ? "Empty" : callNumberPrefix).append(" ")
329                 .append(" ").append(null == enumeration ? "Empty" : enumeration).append(" ")
330                 .append(null == chronology ? "Empty" : chronology).append(" ")
331                 .append(null == barcode ? "Empty" : barcode).append("-").append(null == status ? "Empty" : status).toString();
332 
333         return nonElectronicItemContent;
334     }
335 }