001package org.kuali.ole.describe.bo;
002
003import org.kuali.ole.describe.form.WorkEInstanceOlemlForm;
004import org.kuali.ole.docstore.common.document.content.instance.Location;
005import org.kuali.ole.docstore.common.document.content.instance.LocationLevel;
006import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
007import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
008import org.kuali.rice.krad.service.BusinessObjectService;
009import org.kuali.rice.krad.service.KRADServiceLocator;
010
011import java.util.HashMap;
012import java.util.Map;
013
014/**
015 * Created with IntelliJ IDEA.
016 * User: sambasivam
017 * Date: 9/6/13
018 * Time: 7:24 PM
019 * To change this template use File | Settings | File Templates.
020 */
021public class EInstanceFormDataHandler {
022    private HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
023    public void buildLocationLevels(WorkEInstanceOlemlForm workEInstanceOlemlForm) {
024        OleHoldings eHoldings = workEInstanceOlemlForm.getSelectedEHoldings();
025        Location location = new Location();
026        LocationLevel locationLevel = new LocationLevel();
027
028        String holdingsLocationName = eHoldings.getLocation().getLocationLevel().getName();
029        if (!holdingsLocationName.equalsIgnoreCase("")) {
030            locationLevel = createLocationLevel(holdingsLocationName, locationLevel);
031            location.setLocationLevel(locationLevel);
032            location.setPrimary("true");
033            location.setStatus("permanent");
034            eHoldings.setLocation(location);
035        } else {
036            eHoldings.setLocation(null);
037        }
038
039    }
040
041    public LocationLevel createLocationLevel(String locationName, LocationLevel locationLevel) {
042        if (locationName != null && !locationName.equalsIgnoreCase("")) {
043            BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
044            String[] names = locationName.split("/");
045            Map parentCriteria = new HashMap();
046            parentCriteria.put("locationCode", names[0]);
047            OleLocation oleLocationCollection = businessObjectService.findByPrimaryKey(OleLocation.class, parentCriteria);
048            String locationCode = oleLocationCollection.getLocationCode();
049            String levelCode = oleLocationCollection.getOleLocationLevel().getLevelName();
050            locationLevel.setName(locationCode);
051            locationLevel.setLevel(levelCode);
052            String locName = "";
053            if (locationName.contains("/"))
054                locName = locationName.replace(names[0] + "/", "");
055            else
056                locName = locationName.replace(names[0], "");
057            if (locName != null && !locName.equals("")) {
058                LocationLevel newLocationLevel = new LocationLevel();
059                locationLevel.setLocationLevel(createLocationLevel(locName, newLocationLevel));
060            }
061        }
062        return locationLevel;
063    }
064
065    public String buildEInstanceRecordForDocStore(WorkEInstanceOlemlForm workEInstanceOlemlForm) {
066
067        String content = holdingOlemlRecordProcessor.toXML(workEInstanceOlemlForm.getSelectedEHoldings());
068        return content;
069    }
070
071
072
073    public void setLocationDetails(WorkEInstanceOlemlForm workEInstanceOlemlForm) {
074        OleHoldings eHoldings = workEInstanceOlemlForm.getSelectedEHoldings();
075        if (eHoldings != null) {
076            Location oleHoldingsLocation = eHoldings.getLocation();
077            if (oleHoldingsLocation != null) {
078                LocationLevel holdingsLocationLevel = oleHoldingsLocation.getLocationLevel();
079                String holdingLocationCode = getLocationCode(holdingsLocationLevel);
080                if (holdingsLocationLevel != null) {
081                    eHoldings.getLocation().getLocationLevel().setName(holdingLocationCode);
082                }
083
084            }
085        }
086
087    }
088
089    private String getLocationCode(LocationLevel locationLevel) {
090        String locationCode = "";
091        while (locationLevel != null) {
092            String name = locationLevel.getName();
093            if (name != null) {
094                BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
095                Map parentCriteria = new HashMap();
096                parentCriteria.put("locationCode", name);
097                OleLocation oleLocationCollection = businessObjectService.findByPrimaryKey(OleLocation.class, parentCriteria);
098                if (oleLocationCollection != null) {
099                    String code = oleLocationCollection.getLocationCode();
100                    if (locationCode.equalsIgnoreCase("")) {
101                        locationCode = code;
102                    } else {
103                        locationCode = locationCode + "/" + code;
104                    }
105                }
106            }
107            locationLevel = locationLevel.getLocationLevel();
108        }
109        return locationCode;
110    }
111
112}