001package org.kuali.ole.ncip.service.impl;
002
003import com.thoughtworks.xstream.XStream;
004import org.apache.log4j.Logger;
005import org.extensiblecatalog.ncip.v2.service.*;
006import org.kuali.ole.OLEConstants;
007import org.kuali.ole.deliver.processor.LoanProcessor;
008import org.kuali.ole.ncip.bo.OLECheckInItem;
009import org.kuali.ole.ncip.bo.OLENCIPConstants;
010import org.kuali.ole.ncip.converter.OLECheckInItemConverter;
011import org.kuali.ole.ncip.service.OLECheckInItemService;
012import org.kuali.ole.ncip.service.OLECirculationService;
013import org.kuali.ole.sys.context.SpringContext;
014import org.kuali.rice.core.api.config.property.ConfigContext;
015import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
016
017import java.util.ArrayList;
018import java.util.HashMap;
019import java.util.List;
020
021/**
022 * Created with IntelliJ IDEA.
023 * User: maheswarang
024 * Date: 7/31/13
025 * Time: 1:27 PM
026 * To change this template use File | Settings | File Templates.
027 */
028public class OLECheckInItemServiceImpl implements OLECheckInItemService {
029    private static final Logger LOG = Logger.getLogger(OLECheckInItemServiceImpl.class);
030    private OLECirculationService oleCirculationService;
031    private OLECheckInItemConverter oleCheckInItemConverter;
032    private OLECirculationHelperServiceImpl oleCirculationHelperService;
033    private LoanProcessor loanProcessor;
034
035    public LoanProcessor getLoanProcessor() {
036        if (loanProcessor == null) {
037            loanProcessor = SpringContext.getBean(LoanProcessor.class);
038        }
039        return loanProcessor;
040    }
041
042    public OLECirculationService getOleCirculationService() {
043        if (null == oleCirculationService) {
044            oleCirculationService = GlobalResourceLoader.getService(OLENCIPConstants.CIRCULATION_SERVICE);
045        }
046        return oleCirculationService;
047    }
048
049    public void setOleCirculationService(OLECirculationService oleCirculationService) {
050        this.oleCirculationService = oleCirculationService;
051    }
052
053    public OLECheckInItemConverter getOleCheckInItemConverter() {
054        if (null == oleCheckInItemConverter) {
055            oleCheckInItemConverter = GlobalResourceLoader.getService(OLENCIPConstants.CHECKIN_ITEM_CONVERTER);
056        }
057        return oleCheckInItemConverter;
058    }
059
060    public void setOleCheckInItemConverter(OLECheckInItemConverter oleCheckInItemConverter) {
061        this.oleCheckInItemConverter = oleCheckInItemConverter;
062    }
063
064    public OLECirculationHelperServiceImpl getOleCirculationHelperService() {
065        if (null == oleCirculationHelperService) {
066            oleCirculationHelperService = GlobalResourceLoader.getService(OLENCIPConstants.CIRCULATION_HELPER_SERVICE);
067        }
068        return oleCirculationHelperService;
069    }
070
071    public void setOleCirculationHelperService(OLECirculationHelperServiceImpl oleCirculationHelperService) {
072        this.oleCirculationHelperService = oleCirculationHelperService;
073    }
074
075    @Override
076    public CheckInItemResponseData performService(CheckInItemInitiationData initData, ServiceContext serviceContext, RemoteServiceManager serviceManager) throws ServiceException {
077        CheckInItemResponseData responseData = new CheckInItemResponseData();
078        oleCirculationService = getOleCirculationService();
079        oleCirculationHelperService = getOleCirculationHelperService();
080        oleCheckInItemConverter = getOleCheckInItemConverter();
081        OLECheckInItem oleCheckInItem = null;
082        Problem problem = new Problem();
083        ProblemType problemType = new ProblemType("");
084        List<Problem> problems = new ArrayList<Problem>();
085        String responseString = null;
086        String itemDeleteIndicator = null;
087        AgencyId agencyId = null;
088        String operatorId, itemType = "";
089        if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getFromAgencyId() != null && initData.getInitiationHeader().getFromAgencyId().getAgencyId() != null && initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue() != null && !initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue().trim().isEmpty())
090            agencyId = new AgencyId(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
091        else if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getApplicationProfileType() != null && initData.getInitiationHeader().getApplicationProfileType().getValue() != null && !initData.getInitiationHeader().getApplicationProfileType().getValue().trim().isEmpty())
092            agencyId = new AgencyId(initData.getInitiationHeader().getApplicationProfileType().getValue());
093        else
094            agencyId = new AgencyId(getLoanProcessor().getParameter(OLENCIPConstants.AGENCY_ID_PARAMETER));
095
096        HashMap<String, String> agencyPropertyMap = oleCirculationHelperService.getAgencyPropertyMap(agencyId.getValue());
097        try {
098            if (agencyPropertyMap.size() > 0) {
099                itemType = agencyPropertyMap.get(OLENCIPConstants.ITEM_TYPE);
100                operatorId = agencyPropertyMap.get(OLENCIPConstants.OPERATOR_ID);
101                itemDeleteIndicator=getLoanProcessor().getParameter(OLENCIPConstants.TEMP_ITEM_DELETE_INDICATOR);
102                LOG.info("Inside Check in Item Service . Operator Id : "+operatorId + " Item Id : " + initData.getItemId().getItemIdentifierValue());
103                responseString = oleCirculationService.checkInItem(OLENCIPConstants.PATRON_ID, operatorId, initData.getItemId().getItemIdentifierValue(), itemDeleteIndicator);
104                oleCheckInItem = (OLECheckInItem) oleCheckInItemConverter.generateCheckInItemObject(responseString);
105                if (oleCheckInItem != null && oleCheckInItem.getMessage() != null && oleCheckInItem.getMessage().equals(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.SUCCESSFULLEY_CHECKED_IN))) {
106                    ItemId itemId = new ItemId();
107                    itemId = setItemId(itemId, initData, oleCheckInItem);
108                    responseData.setItemId(itemId);
109                    UserId userId = new UserId();
110                    userId = setUserId(userId, initData, oleCheckInItem);
111                    responseData.setUserId(userId);
112                    ItemOptionalFields itemOptionalFields = new ItemOptionalFields();
113                    itemOptionalFields = setItemOptionalFields(itemOptionalFields, initData, oleCheckInItem);
114                    responseData.setItemOptionalFields(itemOptionalFields);
115                } else {
116                    if (oleCheckInItem != null) {
117                        problem.setProblemDetail(oleCheckInItem.getMessage());
118                    } else {
119                        problem.setProblemDetail(ConfigContext.getCurrentContextConfig().getProperty(OLENCIPConstants.CHECK_IN_FAIL));
120                    }
121                    problem.setProblemElement(OLENCIPConstants.ITEM);
122                    problem.setProblemType(problemType);
123                    problem.setProblemValue("Item value:" + initData.getItemId().getItemIdentifierValue());
124                    problems.add(problem);
125                    responseData.setProblems(problems);
126                }
127            } else {
128                problem.setProblemDetail(ConfigContext.getCurrentContextConfig().getProperty(OLENCIPConstants.INVALID_AGENCY_ID));
129                problem.setProblemElement(OLENCIPConstants.AGENCY_ID);
130                problem.setProblemType(problemType);
131                problem.setProblemValue(agencyId.getValue());
132                problems.add(problem);
133                responseData.setProblems(problems);
134            }
135
136
137        } catch (Exception e) {
138            throw new RuntimeException(e);
139        }
140        return responseData;
141    }
142
143    private ItemId setItemId(ItemId itemId, CheckInItemInitiationData initData, OLECheckInItem oleCheckInItem) throws Exception {
144        AgencyId agencyId = null;
145        if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getFromAgencyId() != null && initData.getInitiationHeader().getFromAgencyId().getAgencyId() != null && initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue() != null && !initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue().trim().isEmpty())
146            agencyId = new AgencyId(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
147        else if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getApplicationProfileType() != null && initData.getInitiationHeader().getApplicationProfileType().getValue() != null && !initData.getInitiationHeader().getApplicationProfileType().getValue().trim().isEmpty())
148            agencyId = new AgencyId(initData.getInitiationHeader().getApplicationProfileType().getValue());
149        else
150            agencyId = new AgencyId(getLoanProcessor().getParameter(OLENCIPConstants.AGENCY_ID_PARAMETER));
151        String identifierType = "";
152        HashMap<String, String> agencyPropertyMap = oleCirculationHelperService.getAgencyPropertyMap(agencyId.getValue());
153        identifierType = agencyPropertyMap.get(OLENCIPConstants.ITEM_TYPE);
154        itemId.setAgencyId(agencyId);
155        ItemIdentifierType itemIdentifierType = new ItemIdentifierType(OLENCIPConstants.SCHEME, identifierType);
156        itemId.setItemIdentifierType(itemIdentifierType);
157        itemId.setItemIdentifierValue(initData.getItemId().getItemIdentifierValue());
158        return itemId;
159    }
160
161    private UserId setUserId(UserId userId, CheckInItemInitiationData initData, OLECheckInItem oleCheckInItem) throws Exception {
162        AgencyId agencyId = null;
163        if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getFromAgencyId() != null && initData.getInitiationHeader().getFromAgencyId().getAgencyId() != null && initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue() != null && !initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue().trim().isEmpty())
164            agencyId = new AgencyId(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
165        else if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getApplicationProfileType() != null && initData.getInitiationHeader().getApplicationProfileType().getValue() != null && !initData.getInitiationHeader().getApplicationProfileType().getValue().trim().isEmpty())
166            agencyId = new AgencyId(initData.getInitiationHeader().getApplicationProfileType().getValue());
167        else
168            agencyId = new AgencyId(getLoanProcessor().getParameter(OLENCIPConstants.AGENCY_ID_PARAMETER));
169        UserIdentifierType userIdentifierType = new UserIdentifierType(oleCheckInItem.getUserType(), oleCheckInItem.getUserType());
170        userId.setUserIdentifierType(userIdentifierType);
171        if (oleCheckInItem.getUserId() != null)
172            userId.setUserIdentifierValue(oleCheckInItem.getUserId());
173        else
174            userId.setUserIdentifierValue(" ");
175        return userId;
176    }
177
178    private ItemOptionalFields setItemOptionalFields(ItemOptionalFields itemOptionalFields, CheckInItemInitiationData initData, OLECheckInItem oleCheckInItem) throws Exception {
179        BibliographicDescription bibliographicDescription = new BibliographicDescription();
180        bibliographicDescription.setAuthor(oleCheckInItem.getTitle());
181        bibliographicDescription.setTitle(oleCheckInItem.getAuthor());
182        itemOptionalFields.setBibliographicDescription(bibliographicDescription);
183        ItemDescription itemDescription = new ItemDescription();
184        itemDescription.setCallNumber(oleCheckInItem.getCallNumber());
185        itemOptionalFields.setItemDescription(itemDescription);
186        return itemOptionalFields;
187    }
188
189
190}