001package org.kuali.ole.service.impl;
002
003import org.apache.log4j.Logger;
004import org.kuali.ole.OLEConstants;
005import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.*;
006import org.kuali.ole.describe.service.DocstoreHelperService;
007import org.kuali.ole.service.OverlayOutputService;
008import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
009
010import java.util.ArrayList;
011import java.util.List;
012
013/**
014 * Created by IntelliJ IDEA.
015 * User: premkb
016 * Date: 2/23/13
017 * Time: 2:53 PM
018 * To change this template use File | Settings | File Templates.
019 */
020public class OverlayDocstoreOutputServiceImpl implements OverlayOutputService {
021    private static final Logger LOG = Logger.getLogger(OverlayDocstoreOutputServiceImpl.class);
022
023    private DocstoreHelperService docstoreHelperService;
024
025    @Override
026    public void setOutPutValue(String field, String value,Object object) {
027        if(object instanceof Instance){
028            Instance instance = (Instance)object;
029            OleHoldings oleHoldings = instance.getOleHoldings();
030            Items items = instance.getItems();
031            instance.setOleHoldings(oleHoldings);
032            instance.setItems(items);
033        }else if(object instanceof OleHoldings){
034            OleHoldings oleHoldings = (OleHoldings)object;
035            if(field.equalsIgnoreCase(OLEConstants.OVERLAY_CALLNUMBER)){
036                CallNumber callNumber =oleHoldings.getCallNumber();
037                if(callNumber==null){
038                    callNumber = new CallNumber();
039                }
040                String callNumberValue = value;
041                callNumber.setClassificationPart(callNumberValue);
042                oleHoldings.setCallNumber(callNumber);
043            }else if(field.equalsIgnoreCase(OLEConstants.OVERLAY_ITEM_LOCATION)){
044                Location location = oleHoldings.getLocation();
045                if(location == null){
046                    location = new Location();
047                }
048                LocationLevel locationLevel = location.getLocationLevel();
049                if(locationLevel == null){
050                    locationLevel = new LocationLevel();
051                }
052                locationLevel.setName(value);
053                location.setLocationLevel(locationLevel);
054                oleHoldings.setLocation(location);
055            }else if(field.equalsIgnoreCase(OLEConstants.OVERLAY_NOTE)) {
056                Note  note = new Note();
057                note.setType(value);
058                List<Note> noteList = new ArrayList<Note>();
059                noteList.add(note);
060                oleHoldings.setNote(noteList);
061            }  else if(field.equalsIgnoreCase(OLEConstants.OVERLAY_HOLDINGS_IDENTIFIER)) {
062                oleHoldings.setHoldingsIdentifier(value);
063            }
064        }else if(object instanceof Item){
065            Item item = (Item)object;
066            if(field.equalsIgnoreCase(OLEConstants.OVERLAY_CALLNUMBER)){
067                CallNumber callNumber =item.getCallNumber();
068                if(callNumber==null){
069                    callNumber = new CallNumber();
070                }
071                String callNumberValue = value;
072                callNumber.setClassificationPart(callNumberValue);
073                item.setCallNumber(callNumber);
074            }else if(field.equalsIgnoreCase(OLEConstants.OVERLAY_ITEM_ITEMTYPE)){
075                ItemType itemType = item.getItemType();
076                if(itemType==null){
077                    itemType = new ItemType();
078                }
079                itemType.setFullValue(value);
080                item.setItemType(itemType);
081
082            }
083            else if(field.equalsIgnoreCase(OLEConstants.OVERLAY_ITEM_BARCODE)){
084                item.setBarcodeARSL(value);
085            }
086            else if(field.equalsIgnoreCase(OLEConstants.OVERLAY_ITEM_STATISTICALSEARCHINGCODES)){
087                StatisticalSearchingCode statisticalSearchingCode = new StatisticalSearchingCode();
088                statisticalSearchingCode.setFullValue(value);
089                List<StatisticalSearchingCode> statisticalSearchingCodeList = new ArrayList<StatisticalSearchingCode>();
090                statisticalSearchingCodeList.add(statisticalSearchingCode);
091                item.setStatisticalSearchingCode(statisticalSearchingCodeList);
092            }
093            else if(field.equalsIgnoreCase(OLEConstants.OVERLAY_ITEM_VENDOR_LINEITEM_IDENTIFIER)){
094                item.setVendorLineItemIdentifier(value);
095            }
096            else if(field.equalsIgnoreCase(OLEConstants.OVERLAY_ITEM_STAFF_ONLY_FLAG)){
097                Boolean staffOnlyFlag = Boolean.parseBoolean(value);
098                item.setStaffOnlyFlag(staffOnlyFlag);
099            }
100            else if(field.equalsIgnoreCase(OLEConstants.OVERLAY_ITEM_FAST_ADD_FLAG)){
101                Boolean fastAddFlag = Boolean.parseBoolean(value);
102                item.setFastAddFlag(fastAddFlag);
103            }
104            else if(field.equalsIgnoreCase(OLEConstants.OVERLAY_ITEM_STATUS)){
105                ItemStatus itemStatus = new ItemStatus();
106                itemStatus.setCodeValue(value);
107                itemStatus.setFullValue(value);
108            }
109            else if(field.equalsIgnoreCase(OLEConstants.OVERLAY_ITEM_LOCATION)){
110                Location location = item.getLocation();
111                if(location == null){
112                    location = new Location();
113                }
114                LocationLevel locationLevel = location.getLocationLevel();
115                if(locationLevel == null){
116                    locationLevel = new LocationLevel();
117                }
118                locationLevel.setName(value);
119                location.setLocationLevel(locationLevel);
120                item.setLocation(location);
121            }
122        }
123    }
124
125
126
127    public void persist(Object object)throws Exception{
128        if(object instanceof InstanceCollection){
129            InstanceCollection instanceCollection = (InstanceCollection)object;
130            getDocstoreHelperService().updateInstanceToDocstore(instanceCollection);
131        }else if(object instanceof OleHoldings){
132            OleHoldings oleHoldings = (OleHoldings)object;
133            getDocstoreHelperService().updateOleHoldingToDocstore(oleHoldings);
134        }else if(object instanceof Items){
135            Items items = (Items)object;
136            List<Item> itemList = items.getItem();
137            for(Item item : itemList){
138                getDocstoreHelperService().updateOleItemToDocstore(item);
139            }
140        }
141    }
142
143    private DocstoreHelperService getDocstoreHelperService(){
144        if (docstoreHelperService == null) {
145            docstoreHelperService = GlobalResourceLoader.getService(OLEConstants.DOCSTORE_HELPER_SERVICE);
146        }
147        return docstoreHelperService;
148    }
149
150    public void setDocstoreHelperService(DocstoreHelperService docstoreHelperService) {
151        this.docstoreHelperService = docstoreHelperService;
152    }
153}