View Javadoc
1   package org.kuali.ole.describe.bo;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.log4j.Logger;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.describe.form.WorkInstanceOlemlForm;
7   import org.kuali.ole.docstore.common.document.content.instance.*;
8   import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
9   import org.kuali.ole.docstore.common.document.content.instance.xstream.InstanceOlemlRecordProcessor;
10  import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
11  import org.kuali.ole.docstore.common.document.content.instance.xstream.SourceHoldingOlemlRecordProcessor;
12  import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
13  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.ControlField;
14  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.DataField;
15  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.SubField;
16  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.WorkBibMarcRecord;
17  import org.kuali.ole.docstore.common.document.content.instance.InstanceCollection;
18  import org.kuali.ole.docstore.common.document.content.instance.Location;
19  import org.kuali.ole.docstore.common.document.content.instance.LocationLevel;
20  import org.kuali.ole.describe.form.InstanceEditorForm;
21  import org.kuali.ole.describe.service.impl.InstanceEditorTreeServiceImpl;
22  import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
23  import org.kuali.rice.krad.service.BusinessObjectService;
24  import org.kuali.rice.krad.service.KRADServiceLocator;
25  import org.kuali.rice.krad.util.GlobalVariables;
26  import org.kuali.rice.krad.web.form.UifFormBase;
27  
28  import java.text.Format;
29  import java.text.SimpleDateFormat;
30  import java.util.*;
31  
32  /**
33   * InstanceEditorFormDataHandler is the data handler class for Instance Editor
34   */
35  public class InstanceEditorFormDataHandler {
36      private static final Logger LOG = Logger.getLogger(InstanceEditorFormDataHandler.class);
37      private static final String loginUser = GlobalVariables.getUserSession().getPrincipalId();
38  
39      private BusinessObjectService businessObjectService;
40  
41      /**
42       * This Method will ingest a new record in docStore for Instance Record type.
43       * returns formatted xml
44       *
45       * @param uifFormBase
46       * @param uuid
47       * @return content
48       */
49      public String buildInstanceRecordForDocStore(UifFormBase uifFormBase, String uuid) {
50          InstanceEditorForm instanceEditorForm = (InstanceEditorForm) uifFormBase;
51          List<Instance> oleInstanceList = new ArrayList<Instance>();
52          InstanceCollection instanceCollection = new InstanceCollection();
53  
54          buildLocationLevels(instanceEditorForm);
55          if (instanceEditorForm.getInstance().getSourceHoldings() != null) {
56              SourceHoldings sourceHoldings = buildSourceHoldings(instanceEditorForm);
57              instanceEditorForm.getInstance().setSourceHoldings(sourceHoldings);
58          }
59          if (instanceEditorForm.getSelectedItem() == null) {
60              Item item = new Item();
61              Items items = new Items();
62              items.setItem(Arrays.asList(item));
63              instanceEditorForm.getInstance().setItems(items);
64          }
65          buildAuditDetailsForNewInstance(instanceEditorForm);
66  
67          oleInstanceList.add(instanceEditorForm.getInstance());
68  
69          instanceCollection.setInstance(oleInstanceList);
70          String content = new InstanceOlemlRecordProcessor().toXML(instanceCollection);
71          return content;
72      }
73  
74      public void buildLocationLevels(InstanceEditorForm instanceEditorForm) {
75          OleHoldings oleHoldings = instanceEditorForm.getSelectedHolding();
76          Location location = new Location();
77          LocationLevel locationLevel = new LocationLevel();
78  
79          String holdingsLocationName = oleHoldings.getLocation().getLocationLevel().getName();
80          if (!holdingsLocationName.equalsIgnoreCase("")) {
81              locationLevel = createLocationLevel(holdingsLocationName, locationLevel);
82              location.setLocationLevel(locationLevel);
83              location.setPrimary("true");
84              location.setStatus("permanent");
85              oleHoldings.setLocation(location);
86          } else {
87              oleHoldings.setLocation(null);
88          }
89  
90          if (instanceEditorForm.getSelectedItem() != null) {
91              Item item = instanceEditorForm.getSelectedItem();
92              Location itemLocation = new Location();
93              LocationLevel itemLocationLevel = new LocationLevel();
94  
95              String itemsLocationName = item.getLocation().getLocationLevel().getName();
96              if (!itemsLocationName.equalsIgnoreCase("")) {
97                  itemLocationLevel = createLocationLevel(itemsLocationName, itemLocationLevel);
98                  itemLocation.setLocationLevel(itemLocationLevel);
99                  itemLocation.setPrimary("true");
100                 itemLocation.setStatus("temporary");
101                 item.setLocation(itemLocation);
102             } else {
103                 item.setLocation(null);
104             }
105         }
106     }
107 
108     public LocationLevel createLocationLevel(String locationName, LocationLevel locationLevel) {
109         if (locationName != null && !locationName.equalsIgnoreCase("")) {
110             BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
111             String[] names = locationName.split("/");
112             Map parentCriteria = new HashMap();
113             parentCriteria.put("locationCode", names[0]);
114             OleLocation oleLocationCollection = businessObjectService.findByPrimaryKey(OleLocation.class, parentCriteria);
115             String levelName = oleLocationCollection.getLocationName();
116             String locationCode = oleLocationCollection.getLocationCode();
117             String levelCode = oleLocationCollection.getOleLocationLevel().getLevelName();
118             locationLevel.setName(locationCode);
119             locationLevel.setLevel(levelCode);
120             String locName = "";
121             if (locationName.contains("/"))
122                 locName = locationName.replace(names[0] + "/", "");
123             else
124                 locName = locationName.replace(names[0], "");
125             if (locName != null && !locName.equals("")) {
126                 LocationLevel newLocationLevel = new LocationLevel();
127                 locationLevel.setLocationLevel(createLocationLevel(locName, newLocationLevel));
128             }
129         }
130         return locationLevel;
131     }
132 
133     public SourceHoldings buildSourceHoldings(InstanceEditorForm instanceEditorForm) {
134         SourceEditorForUI sourceEditorForUI = instanceEditorForm.getSelectedSourceHolding();
135         WorkBibMarcRecord workBibMarcRecord = new WorkBibMarcRecord();
136         workBibMarcRecord.setLeader(sourceEditorForUI.getLeader());
137         workBibMarcRecord.setControlFields(buildBibliographicContrlFields(sourceEditorForUI.getControlFields()));
138         workBibMarcRecord.setDataFields(buildBibliographicDataFields(sourceEditorForUI.getDataFields()));
139 
140         SourceHoldings sourceHoldings = new SourceHoldings();
141         Extension extension = new Extension();
142         extension.getContent().add(workBibMarcRecord);
143         sourceHoldings.setHoldings(extension);
144         sourceHoldings.setHoldingsIdentifier(sourceEditorForUI.getUuid());
145 
146         if (!sourceEditorForUI.getPrimary().equalsIgnoreCase("true")) {
147             sourceHoldings.setPrimary("false");
148         } else {
149             sourceHoldings.setPrimary(sourceEditorForUI.getPrimary());
150         }
151 
152         return sourceHoldings;
153     }
154 
155     private List<ControlField> buildBibliographicContrlFields(List<MarcEditorControlField> controlFields) {
156         List<ControlField> marcControlFieldList = new ArrayList<ControlField>();
157         for (int i = 0; i < controlFields.size(); i++) {
158             ControlField marcControlField = new ControlField();
159             marcControlField.setTag(controlFields.get(i).getTag());
160             marcControlField.setValue(controlFields.get(i).getValue());
161             marcControlFieldList.add(marcControlField);
162         }
163         return marcControlFieldList;
164     }
165 
166     private List<DataField> buildBibliographicDataFields(List<MarcEditorDataField> dataFields) {
167         String marcEditorDataFieldValue;
168         List<DataField> marcDataFieldList = new ArrayList<DataField>();
169         for (int i = 0; i < dataFields.size(); i++) {
170             DataField marcDataField = new DataField();
171             List<SubField> marcSubFieldList = new ArrayList<SubField>();
172             marcEditorDataFieldValue = dataFields.get(i).getValue();
173             StringTokenizer str = new StringTokenizer(marcEditorDataFieldValue, "|");
174             while (str.hasMoreTokens()) {
175                 SubField marcSubField = new SubField();
176                 String dataFieldTokenizedVal = str.nextToken();
177                 String code = Character.toString(dataFieldTokenizedVal.charAt(0));
178                 marcSubField.setCode(code);
179                 String value = dataFieldTokenizedVal.substring(1, dataFieldTokenizedVal.length());
180                 marcSubField.setValue(value);
181                 marcSubFieldList.add(marcSubField);
182             }
183             marcDataField.setSubFields(marcSubFieldList);
184             marcDataField.setTag(dataFields.get(i).getTag());
185             marcDataField.setInd1(dataFields.get(i).getInd1());
186             marcDataField.setInd2(dataFields.get(i).getInd2());
187             marcDataFieldList.add(marcDataField);
188         }
189         return marcDataFieldList;
190     }
191 
192     public SourceEditorForUI buildSourceHoldingForUI(InstanceEditorForm instanceEditorForm) {
193         SourceHoldings sourceHoldings = instanceEditorForm.getInstance().getSourceHoldings();
194         SourceEditorForUI sourceEditorForUI = new SourceEditorForUI();
195         if (sourceHoldings.getHoldings() != null && !sourceHoldings.getHoldings().equals(null) && !sourceHoldings.getHoldings().equals("")) {
196             WorkBibMarcRecord workBibMarcRecord = getWorkBibMarcRecord(sourceHoldings.getHoldings());
197             if (workBibMarcRecord != null) {
198                 sourceEditorForUI.setLeader(workBibMarcRecord.getLeader());
199                 sourceEditorForUI.setControlFields(buildMarcEditorControlFields(workBibMarcRecord.getControlFields()));
200                 sourceEditorForUI.setDataFields(buildMarcEditorDataFields(workBibMarcRecord.getDataFields()));
201             }
202             sourceEditorForUI.setUuid(sourceHoldings.getHoldingsIdentifier());
203             sourceEditorForUI.setPrimary(sourceHoldings.getPrimary());
204 
205             instanceEditorForm.setSelectedSourceHolding(sourceEditorForUI);
206         }
207         return sourceEditorForUI;
208     }
209 
210     public List<MarcEditorControlField> buildMarcEditorControlFields(List<ControlField> controlFields) {
211         List<MarcEditorControlField> marcControlFieldList = new ArrayList<MarcEditorControlField>();
212         for (int i = 0; i < controlFields.size(); i++) {
213             MarcEditorControlField marcEditorControlField = new MarcEditorControlField();
214             marcEditorControlField.setTag(controlFields.get(i).getTag());
215             marcEditorControlField.setValue(controlFields.get(i).getValue());
216             marcControlFieldList.add(marcEditorControlField);
217         }
218         return marcControlFieldList;
219     }
220 
221     public void buildLocationsForUI(InstanceEditorForm instanceEditorForm) {
222         OleHoldings oleHoldings = instanceEditorForm.getInstance().getOleHoldings();
223         LocationLevel locationLevel = oleHoldings.getLocation().getLocationLevel();
224     }
225 
226     public List<MarcEditorDataField> buildMarcEditorDataFields(List<DataField> dataFields) {
227         List<MarcEditorDataField> marcEditorDataFields = new ArrayList<MarcEditorDataField>();
228         for (int i = 0; i < dataFields.size(); i++) {
229             MarcEditorDataField marcEditorDataField = new MarcEditorDataField();
230             marcEditorDataField.setTag(dataFields.get(i).getTag());
231             marcEditorDataField.setInd1(dataFields.get(i).getInd1());
232             marcEditorDataField.setInd2(dataFields.get(i).getInd2());
233             List<SubField> subFields = dataFields.get(i).getSubFields();
234             String subFieldVal = null;
235             for (int j = 0; j < subFields.size(); j++) {
236                 String code = subFields.get(j).getCode();
237                 String value = subFields.get(j).getValue();
238                 if (subFieldVal != null)
239                     subFieldVal = subFieldVal + "|" + code + value;
240                 else
241                     subFieldVal = "|" + code + value;
242             }
243             marcEditorDataField.setValue(subFieldVal);
244             marcEditorDataFields.add(marcEditorDataField);
245         }
246         return marcEditorDataFields;
247     }
248 
249     /**
250      * This Method will build the holdingContent from the docStore and returns the responseXml
251      * based on the itemIdentifier  additionalAttributes has been modified.
252      * toXml method in HoldingOlemlRecordProcessor will convert the oleHolding in to response itemContent.
253      *
254      * @param oleHolding
255      * @return holdingContent
256      */
257     public String buildHoldingContent(OleHoldings oleHolding) {
258         Location location = new Location();
259         LocationLevel locationLevel = new LocationLevel();
260 
261         String holdingsLocationName = (oleHolding.getLocation() != null ? oleHolding.getLocation().getLocationLevel().getName() : "");
262         if (StringUtils.isNotEmpty(holdingsLocationName)) {
263             locationLevel = createLocationLevel(holdingsLocationName, locationLevel);
264             location.setLocationLevel(locationLevel);
265             location.setPrimary("true");
266             location.setStatus("permanent");
267             oleHolding.setLocation(location);
268         } else {
269             oleHolding.setLocation(null);
270         }
271 
272 
273         //Updating Audit details
274 //        AdditionalAttributes additionalAttributes = getFirstAdditionalAttributes(oleHolding.getExtension());
275 //        updateAuditDetails(additionalAttributes);
276 
277         String holdingContent = new HoldingOlemlRecordProcessor().toXML(oleHolding);
278         return holdingContent;
279     }
280 
281     /**
282      * This Method will build the itemContent from the docStore and returns the responseXml
283      * based on the itemIdentifier  additionalAttributes has been modified.
284      * toXml method in ItemOlemlRecordProcessor will convert the oleItem in to response itemContent.
285      *
286      * @param oleItem
287      * @return itemContent
288      */
289     public String buildItemContent(Item oleItem) throws Exception {
290         Location itemLocation = new Location();
291         LocationLevel itemLocationLevel = new LocationLevel();
292 
293         String itemsLocationName = (oleItem.getLocation() != null ? oleItem.getLocation().getLocationLevel().getName() : "");
294         if(StringUtils.isNotEmpty(itemsLocationName)) {
295             if (!itemsLocationName.equalsIgnoreCase("")) {
296                 itemLocationLevel = createLocationLevel(itemsLocationName, itemLocationLevel);
297                 itemLocation.setLocationLevel(itemLocationLevel);
298                 oleItem.setLocation(itemLocation);
299                 itemLocation.setPrimary("true");
300                 itemLocation.setStatus("temporary");
301             }
302         } else {
303             oleItem.setLocation(null);
304         }
305 
306 
307         //compute statistical searching name if statistical searching code is available
308         if (oleItem.getStatisticalSearchingCode().size() > 0) {
309             computeStatisticalSearching(oleItem.getStatisticalSearchingCode().get(0));
310         }
311 
312         //compute item type name if item type code is available
313         computeItemType(oleItem.getItemType());
314 
315         if (oleItem.getTemporaryItemType() != null) {
316             String tempItemTypeFillValue = computeTempItemType(oleItem.getTemporaryItemType());
317             oleItem.getTemporaryItemType().setFullValue(tempItemTypeFillValue);
318         }
319         //Updating Audit details
320         AdditionalAttributes additionalAttributes = null;
321         if ((oleItem.getExtension() != null) &&
322                 oleItem.getExtension().getContent() != null && oleItem.getExtension().getContent().size() > 0) {
323             additionalAttributes = (AdditionalAttributes) oleItem.getExtension().getContent().get(0);
324             updateAuditDetails(additionalAttributes);
325         } else {
326             Extension extension = new Extension();
327             additionalAttributes = new AdditionalAttributes();
328             additionalAttributes.setAttribute(OLEConstants.CREATED_BY, GlobalVariables.getUserSession().getPrincipalName());
329             additionalAttributes.setAttribute(OLEConstants.DATE_ENTERED,
330                     String.valueOf(new SimpleDateFormat("MMM dd, yyyy").format(new Date())));
331             extension.getContent().add(additionalAttributes);
332             oleItem.setExtension(extension);
333         }
334         String itemContent = new ItemOlemlRecordProcessor().toXML(oleItem);
335         return itemContent;
336     }
337 
338     public String buildItemContent(Item oleItem, String staffOnlyFlagForItem) throws Exception {
339         Location itemLocation = new Location();
340         LocationLevel itemLocationLevel = new LocationLevel();
341 
342         String itemsLocationName = (oleItem.getLocation() != null ? oleItem.getLocation().getLocationLevel().getName() : "");
343         if(StringUtils.isNotEmpty(itemsLocationName)) {
344             if (!itemsLocationName.equalsIgnoreCase("")) {
345                 itemLocationLevel = createLocationLevel(itemsLocationName, itemLocationLevel);
346                 itemLocation.setLocationLevel(itemLocationLevel);
347                 oleItem.setLocation(itemLocation);
348                 itemLocation.setPrimary("true");
349                 itemLocation.setStatus("temporary");
350             }
351         } else {
352             oleItem.setLocation(null);
353         }
354         oleItem.setStaffOnlyFlag(Boolean.valueOf(staffOnlyFlagForItem));
355 
356 
357         //compute statistical searching name if statistical searching code is available
358         if (oleItem.getStatisticalSearchingCode().size() > 0) {
359             computeStatisticalSearching(oleItem.getStatisticalSearchingCode().get(0));
360         }
361 
362         //compute item type name if item type code is available
363         computeItemType(oleItem.getItemType());
364 
365         if (oleItem.getTemporaryItemType() != null) {
366             String tempItemTypeFillValue = computeTempItemType(oleItem.getTemporaryItemType());
367             oleItem.getTemporaryItemType().setFullValue(tempItemTypeFillValue);
368         }
369 
370         String itemContent = new ItemOlemlRecordProcessor().toXML(oleItem);
371         return itemContent;
372     }
373 
374 
375     public String buildSourceHoldingContent(SourceHoldings sourceHoldings) throws Exception {
376         //Updating Audit details
377         AdditionalAttributes additionalAttributes = getFirstAdditionalAttributes(sourceHoldings.getExtension());
378         updateAuditDetails(additionalAttributes);
379 
380         String holdingContent = new SourceHoldingOlemlRecordProcessor().toXML(sourceHoldings);
381         return holdingContent;
382     }
383 
384     /**
385      * This Method will build the Left Pane hierarchy details.
386      *
387      * @param instanceEditorForm
388      * @return instanceEditorForm
389      */
390     public InstanceEditorForm buildLeftPaneData(InstanceEditorForm instanceEditorForm) {
391 
392         InstanceEditorTreeServiceImpl InstanceEditorTreeServiceImpl = new InstanceEditorTreeServiceImpl();
393         String treeData = InstanceEditorTreeServiceImpl.createTreeStructure(instanceEditorForm);
394         LOG.info("tree data-->" + treeData);
395         instanceEditorForm.setTreeData(treeData);
396         return instanceEditorForm;
397     }
398 
399     private WorkBibMarcRecord getWorkBibMarcRecord(Extension extension) {
400         for (Object obj : extension.getContent()) {
401             if (obj instanceof WorkBibMarcRecord) {
402                 return (WorkBibMarcRecord) obj;
403             }
404         }
405         return null;
406     }
407 
408     public void setLocationDetails(InstanceEditorForm instanceEditorForm) {
409         OleHoldings oleHoldings = instanceEditorForm.getInstance().getOleHoldings();
410         Location oleHoldingsLocation = oleHoldings.getLocation();
411         if (oleHoldingsLocation != null) {
412             LocationLevel holdingsLocationLevel = oleHoldingsLocation.getLocationLevel();
413             String holdingLocationCode = getLocationCode(holdingsLocationLevel);
414             oleHoldings.getLocation().getLocationLevel().setName(holdingLocationCode);
415         }
416 
417         List<Item> itemList = instanceEditorForm.getInstance().getItems().getItem();
418         for (Item item : itemList) {
419             Location location = item.getLocation();
420             if (location != null) {
421                 LocationLevel itemLocationLevel = location.getLocationLevel();
422                 String itemLocationCode = getLocationCode(itemLocationLevel);
423                 item.getLocation().getLocationLevel().setName(itemLocationCode);
424             }
425         }
426     }
427 
428     public String getLocationCode(LocationLevel locationLevel) {
429         String locationCode = "";
430         while (locationLevel != null) {
431             String name = locationLevel.getName();
432             if (name != null) {
433                 BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
434                 Map parentCriteria = new HashMap();
435                 parentCriteria.put("locationCode", name);
436                 OleLocation oleLocationCollection = businessObjectService.findByPrimaryKey(OleLocation.class, parentCriteria);
437                 if (oleLocationCollection != null) {
438                     String code = oleLocationCollection.getLocationCode();
439                     if (locationCode.equalsIgnoreCase("")) {
440                         locationCode = code;
441                     } else {
442                         locationCode = locationCode + "/" + code;
443                     }
444                 }
445             }
446             locationLevel = locationLevel.getLocationLevel();
447         }
448         return locationCode;
449     }
450 
451     /**
452      * TO set location values in the view
453      *
454      * @param instanceForm
455      */
456     public void setLocationDetails(WorkInstanceOlemlForm instanceForm) {
457         OleHoldings oleHoldings = instanceForm.getSelectedHolding();
458         if (oleHoldings != null) {
459             Location oleHoldingsLocation = oleHoldings.getLocation();
460             if (oleHoldingsLocation != null) {
461                 LocationLevel holdingsLocationLevel = oleHoldingsLocation.getLocationLevel();
462                 String holdingLocationCode = getLocationCode(holdingsLocationLevel);
463                 if (holdingsLocationLevel != null) {
464                     oleHoldings.getLocation().getLocationLevel().setName(holdingLocationCode);
465                 }
466 
467             }
468         }
469         Item item = instanceForm.getSelectedItem();
470         if (item != null) {
471             Location location = item.getLocation();
472             if (location != null) {
473                 LocationLevel itemLocationLevel = location.getLocationLevel();
474                 String itemLocationCode = getLocationCode(itemLocationLevel);
475                 if (itemLocationLevel != null) {
476                     item.getLocation().getLocationLevel().setName(itemLocationCode);
477                 }
478             }
479         }
480     }
481 
482     /**
483      * Method to build Audit Details: Setting Created details for New record and updated details for existing records
484      *
485      * @param instanceEditorForm
486      */
487     private void buildAuditDetailsForNewInstance(InstanceEditorForm instanceEditorForm) {
488         Extension extension = new Extension();
489         AdditionalAttributes additionalAttributes = new AdditionalAttributes();
490         additionalAttributes.setAttribute(OLEConstants.CREATED_BY, loginUser);
491         additionalAttributes.setAttribute(OLEConstants.DATE_ENTERED,
492                 String.valueOf(new SimpleDateFormat("MMM dd, yyyy").format(new Date())));
493         extension.getContent().add(additionalAttributes);
494 
495         if (instanceEditorForm.getSelectedHolding() != null) {
496             instanceEditorForm.getInstance().getOleHoldings().setExtension(extension);
497         } else if (instanceEditorForm.getSelectedSourceHolding() != null) {
498             instanceEditorForm.getInstance().getSourceHoldings().setExtension(extension);
499         }
500         instanceEditorForm.getInstance().getItems().getItem().get(0).setExtension(extension);
501     }
502 
503     private void buildAuditDetailsForNewInstance(WorkInstanceOlemlForm instanceEditorForm, String staffOnlyFlagForHoldings) {
504         Extension extension = new Extension();
505         AdditionalAttributes additionalAttributes = new AdditionalAttributes();
506         additionalAttributes.setAttribute(OLEConstants.CREATED_BY, loginUser);
507         additionalAttributes.setAttribute(OLEConstants.DATE_ENTERED,
508                 String.valueOf(new SimpleDateFormat("MMM dd, yyyy").format(new Date())));
509         additionalAttributes.setAttribute("staffOnlyFlag", staffOnlyFlagForHoldings);
510         additionalAttributes.setAttribute(AdditionalAttributes.HOLDINGS_CREATED_BY,GlobalVariables.getUserSession().getPrincipalName());
511         String holdingsCreatedDate=null;
512         Format formatter = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
513         holdingsCreatedDate = formatter.format(new Date());
514         additionalAttributes.setAttribute(AdditionalAttributes.HOLDINGS_DATE_ENTERED,holdingsCreatedDate);
515         String user = null;
516         user = GlobalVariables.getUserSession().getPrincipalName();
517 
518         additionalAttributes.setAttribute("createdBy", user);
519         extension.getContent().add(additionalAttributes);
520 
521         if (instanceEditorForm.getSelectedHolding() != null) {
522             instanceEditorForm.getInstance().getOleHoldings().setExtension(extension);
523         } else if (instanceEditorForm.getSelectedSourceHolding() != null) {
524             instanceEditorForm.getInstance().getSourceHoldings().setExtension(extension);
525         }
526         //instanceEditorForm.getInstance().getItems().getItem().get(0).setExtension(extension);
527     }
528 
529     /**
530      * Method to set Meta Data fields while loading Holdings/Item record
531      *
532      * @param instanceEditorForm
533      * @param docType
534      * @return instanceEditorForm
535      */
536     public InstanceEditorForm setMetaData(InstanceEditorForm instanceEditorForm, String docType) {
537 
538         if (docType.equalsIgnoreCase(OLEConstants.HOLDING_DOC_TYPE)) {
539             AdditionalAttributes holdingAdditionalAttributes =
540                     getFirstAdditionalAttributes(instanceEditorForm.getInstance().getOleHoldings().getExtension());
541             if (holdingAdditionalAttributes != null) {
542                 instanceEditorForm.getHoldingRecordMetaData().setCreatedBy(
543                         holdingAdditionalAttributes.getAttribute(OLEConstants.CREATED_BY));
544                 instanceEditorForm.getHoldingRecordMetaData().setDateEntered(
545                         holdingAdditionalAttributes.getAttribute(OLEConstants.DATE_ENTERED));
546                 InstanceRecordUpdatedDetails holdingRecordUpdatedDetails = new InstanceRecordUpdatedDetails();
547                 holdingRecordUpdatedDetails.setLastUpdated(holdingAdditionalAttributes.getAttribute(OLEConstants.LAST_UPDATED));
548                 holdingRecordUpdatedDetails.setLastUpdatedBy(holdingAdditionalAttributes.getAttribute(OLEConstants.LAST_UPDATED_BY));
549                 instanceEditorForm.getHoldingRecordMetaData().setUpdatedDetailsList(Arrays.asList(holdingRecordUpdatedDetails));
550             }
551         } else if (docType.equalsIgnoreCase(OLEConstants.ITEM_DOC_TYPE)) {
552             AdditionalAttributes itemAdditionalAttributes = getFirstAdditionalAttributes(
553                     instanceEditorForm.getSelectedItem().getExtension());
554             if (itemAdditionalAttributes != null) {
555                 instanceEditorForm.getItemRecordMetaData().setCreatedBy(
556                         itemAdditionalAttributes.getAttribute(OLEConstants.CREATED_BY));
557                 instanceEditorForm.getItemRecordMetaData().setDateEntered(
558                         itemAdditionalAttributes.getAttribute(OLEConstants.DATE_ENTERED));
559                 InstanceRecordUpdatedDetails itemRecordUpdatedDetails = new InstanceRecordUpdatedDetails();
560                 itemRecordUpdatedDetails.setLastUpdated(itemAdditionalAttributes.getAttribute(OLEConstants.LAST_UPDATED));
561                 itemRecordUpdatedDetails.setLastUpdatedBy(itemAdditionalAttributes.getAttribute(OLEConstants.LAST_UPDATED_BY));
562                 instanceEditorForm.getItemRecordMetaData().setUpdatedDetailsList(Arrays.asList(itemRecordUpdatedDetails));
563             }
564         } else if (docType.equalsIgnoreCase(OLEConstants.SOURCEHOLDINGS_DOC_TYPE)) {
565 
566             AdditionalAttributes sourceHoldingAttributes =
567                     getFirstAdditionalAttributes(instanceEditorForm.getInstance().getSourceHoldings().getExtension());
568             if (sourceHoldingAttributes != null) {
569                 instanceEditorForm.getHoldingRecordMetaData().setCreatedBy(
570                         sourceHoldingAttributes.getAttribute(OLEConstants.CREATED_BY));
571                 instanceEditorForm.getHoldingRecordMetaData().setDateEntered(
572                         sourceHoldingAttributes.getAttribute(OLEConstants.DATE_ENTERED));
573                 InstanceRecordUpdatedDetails holdingRecordUpdatedDetails = new InstanceRecordUpdatedDetails();
574                 holdingRecordUpdatedDetails.setLastUpdated(sourceHoldingAttributes.getAttribute(OLEConstants.LAST_UPDATED));
575                 holdingRecordUpdatedDetails.setLastUpdatedBy(sourceHoldingAttributes.getAttribute(OLEConstants.LAST_UPDATED_BY));
576                 instanceEditorForm.getHoldingRecordMetaData().setUpdatedDetailsList(Arrays.asList(holdingRecordUpdatedDetails));
577             }
578 
579         }
580         return instanceEditorForm;
581     }
582 
583     private void updateAuditDetails(AdditionalAttributes additionalAttributes) {
584         if (additionalAttributes == null) {
585             additionalAttributes = new AdditionalAttributes();
586         }
587         additionalAttributes.setAttribute(OLEConstants.LAST_UPDATED_BY, loginUser);
588         additionalAttributes.setAttribute(OLEConstants.LAST_UPDATED,
589                 String.valueOf(new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a").format(new Date())));
590     }
591 
592     private void updateAuditDetails(AdditionalAttributes additionalAttributes, String staffOnlyFlagForItem) {
593         if (additionalAttributes == null) {
594             additionalAttributes = new AdditionalAttributes();
595         }
596         additionalAttributes.setAttribute(OLEConstants.LAST_UPDATED_BY, loginUser);
597         additionalAttributes.setAttribute(OLEConstants.LAST_UPDATED,
598                 String.valueOf(new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a").format(new Date())));
599         additionalAttributes.setAttribute("staffOnlyFlag", staffOnlyFlagForItem);
600     }
601 
602     private AdditionalAttributes getFirstAdditionalAttributes(Extension extension) {
603         if (extension != null) {
604             for (Object obj : extension.getContent()) {
605                 if (obj instanceof AdditionalAttributes) {
606                     return (AdditionalAttributes) obj;
607                 }
608             }
609         }
610         return null;
611     }
612 
613     /**
614      * This Method will ingest a new record in docStore for Instance Record type.
615      * returns formatted xml
616      *
617      * @param workInstanceOlemlForm
618      * @param uuid
619      * @return content
620      */
621     public String buildInstanceRecordForDocStore(WorkInstanceOlemlForm workInstanceOlemlForm, String uuid) {
622         List<Instance> oleInstanceList = new ArrayList<Instance>();
623         InstanceCollection instanceCollection = new InstanceCollection();
624 
625         buildLocationLevels(workInstanceOlemlForm);
626         if (workInstanceOlemlForm.getInstance().getSourceHoldings() != null) {
627             SourceHoldings sourceHoldings = buildSourceHoldings(workInstanceOlemlForm);
628             workInstanceOlemlForm.getInstance().setSourceHoldings(sourceHoldings);
629         }
630         // Modified if condition for left pane content.
631         if (workInstanceOlemlForm.getSelectedItem() == null) {
632             Item item = new Item();
633             String enumeration = "New";
634             String chronology = "Item";
635             String volumeNumber = "Item";
636             item.setVolumeNumber(volumeNumber);
637             item.setCopyNumber("");
638             item.setCallNumber(item.getCallNumber());
639             Items items = new Items();
640             items.setItem(Arrays.asList(item));
641             workInstanceOlemlForm.getInstance().setItems(items);
642         }
643         buildAuditDetailsForNewInstance(workInstanceOlemlForm);
644 
645         oleInstanceList.add(workInstanceOlemlForm.getInstance());
646 
647         instanceCollection.setInstance(oleInstanceList);
648         String content = new InstanceOlemlRecordProcessor().toXML(instanceCollection);
649         return content;
650     }
651 
652 
653     public String buildInstanceRecordForDocStore(WorkInstanceOlemlForm workInstanceOlemlForm, String uuid, String staffOnlyForHoldings) {
654         List<Instance> oleInstanceList = new ArrayList<Instance>();
655         InstanceCollection instanceCollection = new InstanceCollection();
656 
657         buildLocationLevels(workInstanceOlemlForm);
658         if (workInstanceOlemlForm.getInstance() != null && workInstanceOlemlForm.getInstance().getSourceHoldings() != null) {
659             SourceHoldings sourceHoldings = buildSourceHoldings(workInstanceOlemlForm);
660             workInstanceOlemlForm.getInstance().setSourceHoldings(sourceHoldings);
661         }
662         // Modified if condition for left pane content.
663         if (workInstanceOlemlForm.getSelectedItem() == null) {
664             Item item = new Item();
665             String volumeNumber = "Item";
666             item.setVolumeNumber(volumeNumber);
667             item.setCopyNumber("");
668             item.setCallNumber(item.getCallNumber());
669             Items items = new Items();
670             items.setItem(Arrays.asList(item));
671             workInstanceOlemlForm.getInstance().setItems(items);
672         }
673         buildAuditDetailsForNewInstance(workInstanceOlemlForm, staffOnlyForHoldings);
674 
675         oleInstanceList.add(workInstanceOlemlForm.getInstance());
676 
677         instanceCollection.setInstance(oleInstanceList);
678         String content = new InstanceOlemlRecordProcessor().toXML(instanceCollection);
679         return content;
680     }
681 
682     public void buildLocationLevels(WorkInstanceOlemlForm instanceEditorForm) {
683         OleHoldings oleHoldings = instanceEditorForm.getSelectedHolding();
684         Location location = new Location();
685         LocationLevel locationLevel = new LocationLevel();
686 
687         String holdingsLocationName = oleHoldings.getLocation().getLocationLevel().getName();
688         if (StringUtils.isNotEmpty(holdingsLocationName)) {
689             locationLevel = createLocationLevel(holdingsLocationName, locationLevel);
690             location.setLocationLevel(locationLevel);
691             location.setPrimary("true");
692             location.setStatus("permanent");
693             oleHoldings.setLocation(location);
694         } else {
695             oleHoldings.setLocation(null);
696         }
697 
698         if (instanceEditorForm.getSelectedItem() != null) {
699             Item item = instanceEditorForm.getSelectedItem();
700             Location itemLocation = new Location();
701             LocationLevel itemLocationLevel = new LocationLevel();
702 
703             //String itemsLocationName = item.getLocation().getLocationLevel().getName();
704             if (item.getLocation() != null && item.getLocation().getLocationLevel() != null
705                     && item.getLocation().getLocationLevel().getName() != null) {
706                 if (!item.getLocation().getLocationLevel().getName().equalsIgnoreCase("")) {
707                     itemLocationLevel = createLocationLevel(item.getLocation().getLocationLevel().getName(),
708                             itemLocationLevel);
709                     itemLocation.setLocationLevel(itemLocationLevel);
710                     itemLocation.setPrimary("true");
711                     itemLocation.setStatus("temporary");
712                     item.setLocation(itemLocation);
713                 }
714             } else {
715                 item.setLocation(null);
716                 item.setVolumeNumber("Item");
717                 //  item.setEnumeration("New");
718 
719             }
720             instanceEditorForm.setSelectedItem(item);
721         }
722     }
723 
724     public SourceHoldings buildSourceHoldings(WorkInstanceOlemlForm instanceEditorForm) {
725         SourceEditorForUI sourceEditorForUI = instanceEditorForm.getSelectedSourceHolding();
726         WorkBibMarcRecord workBibMarcRecord = new WorkBibMarcRecord();
727         workBibMarcRecord.setLeader(sourceEditorForUI.getLeader());
728         workBibMarcRecord.setControlFields(buildBibliographicContrlFields(sourceEditorForUI.getControlFields()));
729         workBibMarcRecord.setDataFields(buildBibliographicDataFields(sourceEditorForUI.getDataFields()));
730 
731         SourceHoldings sourceHoldings = new SourceHoldings();
732         Extension extension = new Extension();
733         extension.getContent().add(workBibMarcRecord);
734         sourceHoldings.setHoldings(extension);
735         sourceHoldings.setHoldingsIdentifier(sourceEditorForUI.getUuid());
736 
737         if (!sourceEditorForUI.getPrimary().equalsIgnoreCase("true")) {
738             sourceHoldings.setPrimary("false");
739         } else {
740             sourceHoldings.setPrimary(sourceEditorForUI.getPrimary());
741         }
742 
743         return sourceHoldings;
744     }
745 
746 
747     /**
748      * Method to build Audit Details: Setting Created details for New record and updated details for existing records
749      *
750      * @param workInstanceOlemlForm
751      */
752     private void buildAuditDetailsForNewInstance(WorkInstanceOlemlForm workInstanceOlemlForm) {
753         Extension extension = new Extension();
754         AdditionalAttributes additionalAttributes = new AdditionalAttributes();
755         additionalAttributes.setAttribute(OLEConstants.CREATED_BY, loginUser);
756         additionalAttributes.setAttribute(OLEConstants.DATE_ENTERED,
757                 String.valueOf(new SimpleDateFormat("MMM dd, yyyy").format(new Date())));
758         extension.getContent().add(additionalAttributes);
759 
760         if (workInstanceOlemlForm.getSelectedHolding() != null) {
761             workInstanceOlemlForm.getInstance().getOleHoldings().setExtension(extension);
762         } else if (workInstanceOlemlForm.getSelectedSourceHolding() != null) {
763             workInstanceOlemlForm.getInstance().getSourceHoldings().setExtension(extension);
764         }
765         workInstanceOlemlForm.getInstance().getItems().getItem().get(0).setExtension(extension);
766     }
767 
768     /**
769      * Compute statistical searching name if statistical code value is available.
770      *
771      * @param statisticalSearchingCode
772      */
773     private void computeStatisticalSearching(StatisticalSearchingCode statisticalSearchingCode) {
774         OleStatisticalSearchingCodes oleStatisticalSearchingCodes = new OleStatisticalSearchingCodes();
775         String statisticalSearchingFullValue = "";
776         if (statisticalSearchingCode != null) {
777             if (statisticalSearchingCode.getCodeValue() != null) {
778                 String statisticalSearchingCodeValue = statisticalSearchingCode.getCodeValue();
779                 //statistical searching code is available
780                 if (StringUtils.isNotEmpty(statisticalSearchingCodeValue)) {
781                     BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
782                     Map parentCriteria = new HashMap();
783                     parentCriteria.put("statisticalSearchingCode", statisticalSearchingCodeValue);
784                     oleStatisticalSearchingCodes = businessObjectService
785                             .findByPrimaryKey(OleStatisticalSearchingCodes.class, parentCriteria);
786                     if (oleStatisticalSearchingCodes != null) {
787                         statisticalSearchingFullValue = oleStatisticalSearchingCodes.getStatisticalSearchingName();
788                         statisticalSearchingCode.setFullValue(statisticalSearchingFullValue);
789                     }
790                 }
791             }
792         }
793     }
794 
795 
796     /**
797      * compute item type name if item type code is available
798      *
799      * @param itemType
800      */
801     private void computeItemType(ItemType itemType) {
802         OleInstanceItemType oleInstanceItemType = new OleInstanceItemType();
803         String itemTypeName = "";
804         if (itemType != null) {
805             if (itemType.getCodeValue() != null) {
806                 String itemTypeCodeValue = itemType.getCodeValue();
807                 //item type code is available
808                 if (StringUtils.isNotEmpty(itemTypeCodeValue)) {
809                     BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
810                     Map parentCriteria = new HashMap();
811                     parentCriteria.put("instanceItemTypeCode", itemTypeCodeValue);
812                     oleInstanceItemType = businessObjectService
813                             .findByPrimaryKey(OleInstanceItemType.class, parentCriteria);
814                     if (oleInstanceItemType != null) {
815                         itemTypeName = oleInstanceItemType.getInstanceItemTypeName();
816                         itemType.setFullValue(itemTypeName);
817                     }
818                 }
819             }
820         }
821     }
822 
823     private String computeTempItemType(ItemType itemType) {
824         OleInstanceItemType oleInstanceItemType = new OleInstanceItemType();
825         String tempItemTypeName = "";
826         if (itemType != null) {
827             if (itemType.getCodeValue() != null) {
828                 String tempItemTypeCodeValue = itemType.getCodeValue();
829                 //item type code is available
830                 if (StringUtils.isNotEmpty(tempItemTypeCodeValue)) {
831                     BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
832                     Map parentCriteria = new HashMap();
833                     parentCriteria.put("instanceItemTypeCode", tempItemTypeCodeValue);
834                     oleInstanceItemType = businessObjectService
835                             .findByPrimaryKey(OleInstanceItemType.class, parentCriteria);
836                     if (oleInstanceItemType != null) {
837                         tempItemTypeName = oleInstanceItemType.getInstanceItemTypeName();
838                     }
839                 }
840             }
841         }
842         return tempItemTypeName;
843     }
844     /**
845      * Gets the businessObjectService attribute.
846      *
847      * @return Returns the businessObjectService
848      */
849     private BusinessObjectService getBusinessObjectService() {
850         if (null == businessObjectService) {
851             businessObjectService = KRADServiceLocator.getBusinessObjectService();
852         }
853         return businessObjectService;
854     }
855 
856     public String getParameter(String nameSpace, String component, String name) {
857         String parameter = "";
858         try {
859             Map<String, String> criteriaMap = new HashMap<String, String>();
860             criteriaMap.put("namespaceCode", nameSpace);
861             criteriaMap.put("componentCode", component);
862             criteriaMap.put("name", name);
863             List<ParameterBo> parametersList = (List<ParameterBo>) getBusinessObjectService()
864                     .findMatching(ParameterBo.class, criteriaMap);
865             for (ParameterBo parameterBo : parametersList) {
866                 parameter = parameterBo.getValue();
867             }
868         } catch (Exception e) {
869             LOG.error("Exception:" + e.getMessage(), e);
870         }
871         return parameter;
872     }
873 
874 
875 }