1 package org.kuali.ole.editor.bo;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.ole.OLEConstants;
5 import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
6 import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.ControlField;
7 import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.DataField;
8 import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.SubField;
9 import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.WorkBibMarcRecord;
10 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.*;
11 import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkHoldingOlemlRecordProcessor;
12 import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkInstanceOlemlRecordProcessor;
13 import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkItemOlemlRecordProcessor;
14 import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkSourceHoldingOlemlRecordProcessor;
15 import org.kuali.ole.editor.form.InstanceEditorForm;
16 import org.kuali.ole.editor.service.impl.InstanceEditorTreeServiceImpl;
17 import org.kuali.ole.location.bo.OleLocation;
18 import org.kuali.rice.krad.service.BusinessObjectService;
19 import org.kuali.rice.krad.service.KRADServiceLocator;
20 import org.kuali.rice.krad.util.GlobalVariables;
21 import org.kuali.rice.krad.web.form.UifFormBase;
22
23 import java.text.SimpleDateFormat;
24 import java.util.*;
25
26
27
28
29 public class InstanceEditorFormDataHandler {
30 private static final Logger LOG = Logger.getLogger(InstanceEditorFormDataHandler.class);
31 private static final String loginUser = GlobalVariables.getUserSession().getPrincipalId();
32
33
34
35
36
37
38
39
40
41 public String buildInstanceRecordForDocStore(UifFormBase uifFormBase, String uuid){
42 InstanceEditorForm instanceEditorForm = (InstanceEditorForm) uifFormBase;
43 List<Instance> oleInstanceList = new ArrayList<Instance>();
44 InstanceCollection instanceCollection = new InstanceCollection();
45
46 buildLocationLevels(instanceEditorForm);
47 if(instanceEditorForm.getInstance().getSourceHoldings() != null){
48 SourceHoldings sourceHoldings = buildSourceHoldings(instanceEditorForm);
49 instanceEditorForm.getInstance().setSourceHoldings(sourceHoldings);
50 }
51 if(instanceEditorForm.getSelectedItem()==null){
52 Item item = new Item();
53 Items items = new Items();
54 items.setItem(Arrays.asList(item));
55 instanceEditorForm.getInstance().setItems(items);
56 }
57 buildAuditDetailsForNewInstance(instanceEditorForm);
58
59 oleInstanceList.add(instanceEditorForm.getInstance());
60
61 instanceCollection.setInstance(oleInstanceList);
62 String content = new WorkInstanceOlemlRecordProcessor().toXML(instanceCollection);
63 return content;
64 }
65
66 public void buildLocationLevels(InstanceEditorForm instanceEditorForm) {
67 OleHoldings oleHoldings = instanceEditorForm.getSelectedHolding();
68 Location location = new Location();
69 LocationLevel locationLevel = new LocationLevel();
70
71 String holdingsLocationName = oleHoldings.getLocation().getLocationLevel().getName();
72 if (!holdingsLocationName.equalsIgnoreCase("")) {
73 locationLevel = createLocationLevel(holdingsLocationName, locationLevel);
74 location.setLocationLevel(locationLevel);
75 location.setPrimary("true");
76 location.setStatus("permanent");
77 oleHoldings.setLocation(location);
78 } else {
79 oleHoldings.setLocation(null);
80 }
81
82 if(instanceEditorForm.getSelectedItem() != null){
83 Item item = instanceEditorForm.getSelectedItem();
84 Location itemLocation = new Location();
85 LocationLevel itemLocationLevel = new LocationLevel();
86
87 String itemsLocationName = item.getLocation().getLocationLevel().getName();
88 if (!itemsLocationName.equalsIgnoreCase("")) {
89 itemLocationLevel = createLocationLevel(itemsLocationName, itemLocationLevel);
90 itemLocation.setLocationLevel(itemLocationLevel);
91 itemLocation.setPrimary("true");
92 itemLocation.setStatus("temporary");
93 item.setLocation(itemLocation);
94 } else {
95 item.setLocation(null);
96 }
97 }
98 }
99
100 public LocationLevel createLocationLevel(String locationName, LocationLevel locationLevel) {
101 if(locationName!=null && !locationName.equalsIgnoreCase("")){
102 BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
103 String[] names = locationName.split("/");
104 Map parentCriteria = new HashMap();
105 parentCriteria.put("locationCode",names[0]);
106 OleLocation oleLocationCollection = businessObjectService.findByPrimaryKey(OleLocation.class, parentCriteria);
107 String levelName = oleLocationCollection.getLocationName();
108 String locationCode = oleLocationCollection.getLocationCode();
109 String levelCode = oleLocationCollection.getOleLocationLevel().getLevelName();
110 locationLevel.setName(locationCode);
111 locationLevel.setLevel(levelCode);
112 String locName = "";
113 if(locationName.contains("/"))
114 locName = locationName.replace(names[0]+"/","");
115 else
116 locName = locationName.replace(names[0],"");
117 if(locName!=null && ! locName.equals("")){
118 LocationLevel newLocationLevel=new LocationLevel();
119 locationLevel.setLocationLevel(createLocationLevel(locName,newLocationLevel));
120 }
121 }
122 return locationLevel;
123 }
124
125 public SourceHoldings buildSourceHoldings(InstanceEditorForm instanceEditorForm) {
126 SourceEditorForUI sourceEditorForUI = instanceEditorForm.getSelectedSourceHolding();
127 WorkBibMarcRecord workBibMarcRecord = new WorkBibMarcRecord();
128 workBibMarcRecord.setLeader(sourceEditorForUI.getLeader());
129 workBibMarcRecord.setControlFields(buildBibliographicContrlFields(sourceEditorForUI.getControlFields()));
130 workBibMarcRecord.setDataFields(buildBibliographicDataFields(sourceEditorForUI.getDataFields()));
131
132 SourceHoldings sourceHoldings = new SourceHoldings();
133 Extension extension = new Extension();
134 extension.getContent().add(workBibMarcRecord);
135 sourceHoldings.setHoldings(extension);
136 sourceHoldings.setHoldingsIdentifier(sourceEditorForUI.getUuid());
137
138 if (!sourceEditorForUI.getPrimary().equalsIgnoreCase("true")) {
139 sourceHoldings.setPrimary("false");
140 } else {
141 sourceHoldings.setPrimary(sourceEditorForUI.getPrimary());
142 }
143
144 return sourceHoldings;
145 }
146
147 private List<ControlField> buildBibliographicContrlFields(List<MarcEditorControlField> controlFields) {
148 List<ControlField> marcControlFieldList = new ArrayList<ControlField>();
149 for (int i = 0; i < controlFields.size(); i++) {
150 ControlField marcControlField = new ControlField();
151 marcControlField.setTag(controlFields.get(i).getTag());
152 marcControlField.setValue(controlFields.get(i).getValue());
153 marcControlFieldList.add(marcControlField);
154 }
155 return marcControlFieldList;
156 }
157
158 private List<DataField> buildBibliographicDataFields(List<MarcEditorDataField> dataFields) {
159 String marcEditorDataFieldValue;
160 List<DataField> marcDataFieldList = new ArrayList<DataField>();
161 for (int i = 0; i < dataFields.size(); i++) {
162 DataField marcDataField = new DataField();
163 List<SubField> marcSubFieldList = new ArrayList<SubField>();
164 marcEditorDataFieldValue = dataFields.get(i).getValue();
165 StringTokenizer str = new StringTokenizer(marcEditorDataFieldValue, "|");
166 while (str.hasMoreTokens()) {
167 SubField marcSubField = new SubField();
168 String dataFieldTokenizedVal = str.nextToken();
169 String code = Character.toString(dataFieldTokenizedVal.charAt(0));
170 marcSubField.setCode(code);
171 String value = dataFieldTokenizedVal.substring(1, dataFieldTokenizedVal.length());
172 marcSubField.setValue(value);
173 marcSubFieldList.add(marcSubField);
174 }
175 marcDataField.setSubFields(marcSubFieldList);
176 marcDataField.setTag(dataFields.get(i).getTag());
177 marcDataField.setInd1(dataFields.get(i).getInd1());
178 marcDataField.setInd2(dataFields.get(i).getInd2());
179 marcDataFieldList.add(marcDataField);
180 }
181 return marcDataFieldList;
182 }
183
184 public SourceEditorForUI buildSourceHoldingForUI(InstanceEditorForm instanceEditorForm) {
185 SourceHoldings sourceHoldings = instanceEditorForm.getInstance().getSourceHoldings();
186 SourceEditorForUI sourceEditorForUI = new SourceEditorForUI();
187 if(sourceHoldings.getHoldings() != null && !sourceHoldings.getHoldings().equals(null) && !sourceHoldings.getHoldings().equals("")) {
188 WorkBibMarcRecord workBibMarcRecord = getWorkBibMarcRecord(sourceHoldings.getHoldings());
189 if (workBibMarcRecord != null) {
190 sourceEditorForUI.setLeader(workBibMarcRecord.getLeader());
191 sourceEditorForUI.setControlFields(buildMarcEditorControlFields(workBibMarcRecord.getControlFields()));
192 sourceEditorForUI.setDataFields(buildMarcEditorDataFields(workBibMarcRecord.getDataFields()));
193 }
194 sourceEditorForUI.setUuid(sourceHoldings.getHoldingsIdentifier());
195 sourceEditorForUI.setPrimary(sourceHoldings.getPrimary());
196
197 instanceEditorForm.setSelectedSourceHolding(sourceEditorForUI);
198 }
199 return sourceEditorForUI;
200 }
201
202 public List<MarcEditorControlField> buildMarcEditorControlFields(List<ControlField> controlFields) {
203 List<MarcEditorControlField> marcControlFieldList = new ArrayList<MarcEditorControlField>();
204 for (int i = 0; i < controlFields.size(); i++) {
205 MarcEditorControlField marcEditorControlField = new MarcEditorControlField();
206 marcEditorControlField.setTag(controlFields.get(i).getTag());
207 marcEditorControlField.setValue(controlFields.get(i).getValue());
208 marcControlFieldList.add(marcEditorControlField);
209 }
210 return marcControlFieldList;
211 }
212
213 public void buildLocationsForUI(InstanceEditorForm instanceEditorForm) {
214 OleHoldings oleHoldings = instanceEditorForm.getInstance().getOleHoldings();
215 LocationLevel locationLevel = oleHoldings.getLocation().getLocationLevel();
216 }
217
218 public List<MarcEditorDataField> buildMarcEditorDataFields(List<DataField> dataFields) {
219 List<MarcEditorDataField> marcEditorDataFields = new ArrayList<MarcEditorDataField>();
220 for (int i = 0; i < dataFields.size(); i++) {
221 MarcEditorDataField marcEditorDataField = new MarcEditorDataField();
222 marcEditorDataField.setTag(dataFields.get(i).getTag());
223 marcEditorDataField.setInd1(dataFields.get(i).getInd1());
224 marcEditorDataField.setInd2(dataFields.get(i).getInd2());
225 List<SubField> subFields = dataFields.get(i).getSubFields();
226 String subFieldVal = null;
227 for (int j = 0; j < subFields.size(); j++) {
228 String code = subFields.get(j).getCode();
229 String value = subFields.get(j).getValue();
230 if (subFieldVal != null)
231 subFieldVal = subFieldVal + "|" + code + value;
232 else
233 subFieldVal = "|" + code + value;
234 }
235 marcEditorDataField.setValue(subFieldVal);
236 marcEditorDataFields.add(marcEditorDataField);
237 }
238 return marcEditorDataFields;
239 }
240
241
242
243
244
245
246
247
248 public String buildHoldingContent (OleHoldings oleHolding) throws Exception {
249 Location location = new Location();
250 LocationLevel locationLevel = new LocationLevel();
251
252 String holdingsLocationName = (oleHolding.getLocation() != null ? oleHolding.getLocation().getLocationLevel().getName() : "");
253 if (!holdingsLocationName.equalsIgnoreCase("")) {
254 locationLevel = createLocationLevel(holdingsLocationName, locationLevel);
255 location.setLocationLevel(locationLevel);
256 location.setPrimary("true");
257 location.setStatus("permanent");
258 oleHolding.setLocation(location);
259 } else {
260 oleHolding.setLocation(null);
261 }
262
263 AdditionalAttributes additionalAttributes = getFirstAdditionalAttributes(oleHolding.getExtension());
264 updateAuditDetails(additionalAttributes);
265
266 String holdingContent = new WorkHoldingOlemlRecordProcessor().toXML(oleHolding);
267 return holdingContent;
268 }
269
270
271
272
273
274
275
276
277 public String buildItemContent (Item oleItem) throws Exception {
278 Location itemLocation = new Location();
279 LocationLevel itemLocationLevel = new LocationLevel();
280
281 String itemsLocationName = (oleItem.getLocation() != null ? oleItem.getLocation().getLocationLevel().getName(): "");
282 if (!itemsLocationName.equalsIgnoreCase("")) {
283 itemLocationLevel = createLocationLevel(itemsLocationName, itemLocationLevel);
284 itemLocation.setLocationLevel(itemLocationLevel);
285 oleItem.setLocation(itemLocation);
286 itemLocation.setPrimary("true");
287 itemLocation.setStatus("temporary");
288 } else {
289 oleItem.setLocation(null);
290 }
291
292
293 AdditionalAttributes additionalAttributes = null;
294 if ((oleItem.getExtension() != null) &&
295 oleItem.getExtension().getContent() != null && oleItem.getExtension().getContent().size() > 0) {
296 additionalAttributes = (AdditionalAttributes) oleItem.getExtension().getContent().get(0);
297 updateAuditDetails(additionalAttributes);
298 } else {
299 Extension extension = new Extension();
300 additionalAttributes = new AdditionalAttributes();
301 additionalAttributes.setAttribute(OLEConstants.CREATED_BY, loginUser);
302 additionalAttributes.setAttribute(OLEConstants.DATE_ENTERED,
303 String.valueOf(new SimpleDateFormat("MMM dd, yyyy").format(new Date())));
304 extension.getContent().add(additionalAttributes);
305 oleItem.setExtension(extension);
306 }
307 String itemContent = new WorkItemOlemlRecordProcessor().toXML(oleItem);
308 return itemContent;
309 }
310
311 public String buildSourceHoldingContent (SourceHoldings sourceHoldings) throws Exception {
312
313 AdditionalAttributes additionalAttributes = getFirstAdditionalAttributes(sourceHoldings.getExtension());
314 updateAuditDetails(additionalAttributes);
315
316 String holdingContent = new WorkSourceHoldingOlemlRecordProcessor().toXML(sourceHoldings);
317 return holdingContent;
318 }
319
320
321
322
323
324
325 public InstanceEditorForm buildLeftPaneData(InstanceEditorForm instanceEditorForm) {
326
327 InstanceEditorTreeServiceImpl InstanceEditorTreeServiceImpl = new InstanceEditorTreeServiceImpl();
328 String treeData = InstanceEditorTreeServiceImpl.createTreeStructure(instanceEditorForm);
329 LOG.info("tree data-->"+treeData);
330 instanceEditorForm.setTreeData(treeData);
331 return instanceEditorForm;
332 }
333
334 private WorkBibMarcRecord getWorkBibMarcRecord(Extension extension) {
335 for (Object obj : extension.getContent()) {
336 if (obj instanceof WorkBibMarcRecord) {
337 return (WorkBibMarcRecord) obj;
338 }
339 }
340 return null;
341 }
342
343 public void setLocationDetails(InstanceEditorForm instanceEditorForm) {
344 OleHoldings oleHoldings = instanceEditorForm.getInstance().getOleHoldings();
345 Location oleHoldingsLocation = oleHoldings.getLocation();
346 if (oleHoldingsLocation != null) {
347 LocationLevel holdingsLocationLevel = oleHoldingsLocation.getLocationLevel();
348 String holdingLocationCode = getLocationCode(holdingsLocationLevel);
349 oleHoldings.getLocation().getLocationLevel().setName(holdingLocationCode);
350 }
351
352 List<Item> itemList = instanceEditorForm.getInstance().getItems().getItem();
353 for (Item item : itemList) {
354 Location location =item.getLocation();
355 if(location != null){
356 LocationLevel itemLocationLevel = location.getLocationLevel();
357 String itemLocationCode = getLocationCode(itemLocationLevel);
358 item.getLocation().getLocationLevel().setName(itemLocationCode);
359 }
360 }
361 }
362
363 private String getLocationCode(LocationLevel locationLevel) {
364 String locationCode = "";
365 while (locationLevel != null) {
366 String name = locationLevel.getName();
367 if(name != null){
368 BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
369 Map parentCriteria = new HashMap();
370 parentCriteria.put("locationCode",name);
371 OleLocation oleLocationCollection = businessObjectService.findByPrimaryKey(OleLocation.class, parentCriteria);
372 if (oleLocationCollection != null) {
373 String code = oleLocationCollection.getLocationCode();
374 if (locationCode.equalsIgnoreCase("")) {
375 locationCode = code;
376 } else {
377 locationCode = locationCode + "/" + code;
378 }
379 }
380 }
381 locationLevel = locationLevel.getLocationLevel();
382 }
383 return locationCode;
384 }
385
386
387
388
389
390 private void buildAuditDetailsForNewInstance(InstanceEditorForm instanceEditorForm) {
391 Extension extension = new Extension();
392 AdditionalAttributes additionalAttributes = new AdditionalAttributes();
393 additionalAttributes.setAttribute(OLEConstants.CREATED_BY, loginUser);
394 additionalAttributes.setAttribute(OLEConstants.DATE_ENTERED,
395 String.valueOf(new SimpleDateFormat("MMM dd, yyyy").format(new Date())));
396 extension.getContent().add(additionalAttributes);
397
398 if (instanceEditorForm.getSelectedHolding() != null) {
399 instanceEditorForm.getInstance().getOleHoldings().setExtension(extension);
400 } else if (instanceEditorForm.getSelectedSourceHolding() != null) {
401 instanceEditorForm.getInstance().getSourceHoldings().setExtension(extension);
402 }
403 instanceEditorForm.getInstance().getItems().getItem().get(0).setExtension(extension);
404 }
405
406
407
408
409
410
411
412 public InstanceEditorForm setMetaData(InstanceEditorForm instanceEditorForm, String docType) {
413
414 if (docType.equalsIgnoreCase(OLEConstants.HOLDING_DOC_TYPE)) {
415 AdditionalAttributes holdingAdditionalAttributes =
416 getFirstAdditionalAttributes(instanceEditorForm.getInstance().getOleHoldings().getExtension());
417 if (holdingAdditionalAttributes != null) {
418 instanceEditorForm.getHoldingRecordMetaData().setCreatedBy(
419 holdingAdditionalAttributes.getAttribute(OLEConstants.CREATED_BY));
420 instanceEditorForm.getHoldingRecordMetaData().setDateEntered(
421 holdingAdditionalAttributes.getAttribute(OLEConstants.DATE_ENTERED));
422 InstanceRecordUpdatedDetails holdingRecordUpdatedDetails = new InstanceRecordUpdatedDetails();
423 holdingRecordUpdatedDetails.setLastUpdated(holdingAdditionalAttributes.getAttribute(OLEConstants.LAST_UPDATED));
424 holdingRecordUpdatedDetails.setLastUpdatedBy(holdingAdditionalAttributes.getAttribute(OLEConstants.LAST_UPDATED_BY));
425 instanceEditorForm.getHoldingRecordMetaData().setUpdatedDetailsList(Arrays.asList(holdingRecordUpdatedDetails));
426 }
427 } else if (docType.equalsIgnoreCase(OLEConstants.ITEM_DOC_TYPE)) {
428 AdditionalAttributes itemAdditionalAttributes = getFirstAdditionalAttributes(
429 instanceEditorForm.getSelectedItem().getExtension());
430 if (itemAdditionalAttributes != null) {
431 instanceEditorForm.getItemRecordMetaData().setCreatedBy(
432 itemAdditionalAttributes.getAttribute(OLEConstants.CREATED_BY));
433 instanceEditorForm.getItemRecordMetaData().setDateEntered(
434 itemAdditionalAttributes.getAttribute(OLEConstants.DATE_ENTERED));
435 InstanceRecordUpdatedDetails itemRecordUpdatedDetails = new InstanceRecordUpdatedDetails();
436 itemRecordUpdatedDetails.setLastUpdated(itemAdditionalAttributes.getAttribute(OLEConstants.LAST_UPDATED));
437 itemRecordUpdatedDetails.setLastUpdatedBy(itemAdditionalAttributes.getAttribute(OLEConstants.LAST_UPDATED_BY));
438 instanceEditorForm.getItemRecordMetaData().setUpdatedDetailsList(Arrays.asList(itemRecordUpdatedDetails));
439 }
440 } else if (docType.equalsIgnoreCase(OLEConstants.SOURCEHOLDINGS_DOC_TYPE)) {
441
442 AdditionalAttributes sourceHoldingAttributes =
443 getFirstAdditionalAttributes(instanceEditorForm.getInstance().getSourceHoldings().getExtension());
444 if (sourceHoldingAttributes != null) {
445 instanceEditorForm.getHoldingRecordMetaData().setCreatedBy(
446 sourceHoldingAttributes.getAttribute(OLEConstants.CREATED_BY));
447 instanceEditorForm.getHoldingRecordMetaData().setDateEntered(
448 sourceHoldingAttributes.getAttribute(OLEConstants.DATE_ENTERED));
449 InstanceRecordUpdatedDetails holdingRecordUpdatedDetails = new InstanceRecordUpdatedDetails();
450 holdingRecordUpdatedDetails.setLastUpdated(sourceHoldingAttributes.getAttribute(OLEConstants.LAST_UPDATED));
451 holdingRecordUpdatedDetails.setLastUpdatedBy(sourceHoldingAttributes.getAttribute(OLEConstants.LAST_UPDATED_BY));
452 instanceEditorForm.getHoldingRecordMetaData().setUpdatedDetailsList(Arrays.asList(holdingRecordUpdatedDetails));
453 }
454
455 }
456 return instanceEditorForm;
457 }
458
459 private void updateAuditDetails(AdditionalAttributes additionalAttributes) {
460 if (additionalAttributes == null) {
461 additionalAttributes = new AdditionalAttributes();
462 }
463 additionalAttributes.setAttribute(OLEConstants.LAST_UPDATED_BY, loginUser);
464 additionalAttributes.setAttribute(OLEConstants.LAST_UPDATED,
465 String.valueOf(new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a").format(new Date())));
466 }
467
468 private AdditionalAttributes getFirstAdditionalAttributes(Extension extension) {
469 if(extension != null){
470 for (Object obj : extension.getContent()) {
471 if (obj instanceof AdditionalAttributes) {
472 return (AdditionalAttributes) obj;
473 }
474 }
475 }
476 return null;
477 }
478 }