001package org.kuali.ole.ncip.service.impl;
002
003import org.apache.log4j.Logger;
004import org.extensiblecatalog.ncip.v2.service.*;
005import org.kuali.ole.OLEConstants;
006import org.kuali.ole.deliver.processor.LoanProcessor;
007import org.kuali.ole.ncip.bo.OLECheckOutItem;
008import org.kuali.ole.ncip.bo.OLENCIPConstants;
009import org.kuali.ole.ncip.converter.OLECheckOutItemConverter;
010import org.kuali.ole.ncip.service.OLECheckOutItemService;
011import org.kuali.ole.ncip.service.OLECirculationService;
012import org.kuali.ole.sys.context.SpringContext;
013import org.kuali.rice.core.api.config.property.ConfigContext;
014import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
015
016import java.math.BigDecimal;
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:29 PM
026 * To change this template use File | Settings | File Templates.
027 */
028public class OLECheckOutItemServiceImpl implements OLECheckOutItemService {
029    private static final Logger LOG = Logger.getLogger(OLECheckOutItemServiceImpl.class);
030    private OLECirculationService oleCirculationService;
031    private OLECheckOutItemConverter oleCheckOutItemConverter;
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 OLECheckOutItemConverter getOleCheckOutItemConverter() {
054        if (null == oleCheckOutItemConverter) {
055            oleCheckOutItemConverter = GlobalResourceLoader.getService(OLENCIPConstants.CHECKOUT_ITEM_CONVERTER);
056        }
057        return oleCheckOutItemConverter;
058    }
059
060    public void setOleCheckOutItemConverter(OLECheckOutItemConverter oleCheckOutItemConverter) {
061        this.oleCheckOutItemConverter = oleCheckOutItemConverter;
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 CheckOutItemResponseData performService(CheckOutItemInitiationData initData, ServiceContext serviceContext, RemoteServiceManager serviceManager) throws ServiceException, ValidationException {
077        CheckOutItemResponseData responseData = new CheckOutItemResponseData();
078        oleCirculationService = getOleCirculationService();
079        oleCirculationHelperService = getOleCirculationHelperService();
080        oleCheckOutItemConverter = getOleCheckOutItemConverter();
081        OLECheckOutItem oleCheckOutItem = null;
082        Problem problem = new Problem();
083        ProblemType problemType = new ProblemType("");
084        List<Problem> problems = new ArrayList<Problem>();
085        String responseString = null;
086        AgencyId agencyId = null;
087        String operatorId, itemType = "";
088        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())
089            agencyId = new AgencyId(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
090        else if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getApplicationProfileType() != null && initData.getInitiationHeader().getApplicationProfileType().getValue() != null && !initData.getInitiationHeader().getApplicationProfileType().getValue().trim().isEmpty())
091            agencyId = new AgencyId(initData.getInitiationHeader().getApplicationProfileType().getValue());
092        else
093            agencyId = new AgencyId(getLoanProcessor().getParameter(OLENCIPConstants.AGENCY_ID_PARAMETER));
094        HashMap<String, String> agencyPropertyMap = oleCirculationHelperService.getAgencyPropertyMap(agencyId.getValue());
095        try {
096            if (agencyPropertyMap.size() > 0) {
097                itemType = agencyPropertyMap.get(OLENCIPConstants.ITEM_TYPE);
098                operatorId = agencyPropertyMap.get(OLENCIPConstants.OPERATOR_ID);
099                LOG.info("Inside Check Out Item Service . Patron Barcode : " + initData.getUserId().getUserIdentifierValue() + " Operator Id : "+ operatorId + " Item Barcode : "+ initData.getItemId().getItemIdentifierValue() );
100                responseString = oleCirculationService.checkOutItem(initData.getUserId().getUserIdentifierValue(), operatorId, initData.getItemId().getItemIdentifierValue(),false);
101                oleCheckOutItem = (OLECheckOutItem) oleCheckOutItemConverter.generateCheckoutItemObject(responseString);
102                if (oleCheckOutItem != null && oleCheckOutItem.getMessage() != null && oleCheckOutItem.getMessage().equals(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.SUCCESSFULLEY_LOANED))) {
103                    ItemId itemId = new ItemId();
104                    itemId = setItemId(itemId, initData, oleCheckOutItem);
105                    responseData.setRenewalCount(new BigDecimal(oleCheckOutItem.getRenewalCount()));
106                    responseData.setDateDue(oleCirculationHelperService.getGregorianCalendarDate(oleCheckOutItem.getDueDate()));
107                    responseData.setItemId(itemId);
108                    UserId userId = new UserId();
109                    userId = setUserId(userId, initData, oleCheckOutItem);
110                    responseData.setUserId(userId);
111                } else {
112                    if (oleCheckOutItem != null) {
113                        problem.setProblemDetail(oleCheckOutItem.getMessage());
114                    } else {
115                        problem.setProblemDetail(ConfigContext.getCurrentContextConfig().getProperty(OLENCIPConstants.CHECK_OUT_FAIL));
116                    }
117                    problem.setProblemElement(OLENCIPConstants.ITEM);
118                    problem.setProblemType(problemType);
119                    problem.setProblemValue(initData.getItemId().getItemIdentifierValue());
120                    problems.add(problem);
121                    responseData.setProblems(problems);
122                }
123            } else {
124                problem.setProblemDetail(ConfigContext.getCurrentContextConfig().getProperty(OLENCIPConstants.INVALID_AGENCY_ID));
125                problem.setProblemElement(OLENCIPConstants.AGENCY_ID);
126                problem.setProblemType(problemType);
127                problem.setProblemValue("Agency Id:" + agencyId.getValue());
128                problems.add(problem);
129                responseData.setProblems(problems);
130            }
131
132
133        } catch (Exception e) {
134            throw new RuntimeException(e);
135        }
136        return responseData;
137    }
138
139    private ItemId setItemId(ItemId itemId, CheckOutItemInitiationData initData, OLECheckOutItem oleCheckOutItem) throws Exception {
140        AgencyId agencyId = null;
141        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())
142            agencyId = new AgencyId(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
143        else if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getApplicationProfileType() != null && initData.getInitiationHeader().getApplicationProfileType().getValue() != null && !initData.getInitiationHeader().getApplicationProfileType().getValue().trim().isEmpty())
144            agencyId = new AgencyId(initData.getInitiationHeader().getApplicationProfileType().getValue());
145        else
146            agencyId = new AgencyId(getLoanProcessor().getParameter(OLENCIPConstants.AGENCY_ID_PARAMETER));
147        String identifierType = "";
148        HashMap<String, String> agencyPropertyMap = oleCirculationHelperService.getAgencyPropertyMap(agencyId.getValue());
149        identifierType = agencyPropertyMap.get(OLENCIPConstants.ITEM_TYPE);
150        itemId.setAgencyId(agencyId);
151        ItemIdentifierType itemIdentifierType = new ItemIdentifierType(OLENCIPConstants.SCHEME, identifierType);
152        itemId.setItemIdentifierType(itemIdentifierType);
153        itemId.setItemIdentifierValue(initData.getItemId().getItemIdentifierValue());
154        return itemId;
155    }
156
157    private UserId setUserId(UserId userId, CheckOutItemInitiationData initData, OLECheckOutItem oleCheckOutItem) throws Exception {
158        AgencyId agencyId = null;
159        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())
160            agencyId = new AgencyId(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
161        else if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getApplicationProfileType() != null && initData.getInitiationHeader().getApplicationProfileType().getValue() != null && !initData.getInitiationHeader().getApplicationProfileType().getValue().trim().isEmpty())
162            agencyId = new AgencyId(initData.getInitiationHeader().getApplicationProfileType().getValue());
163        else
164            agencyId = new AgencyId(getLoanProcessor().getParameter(OLENCIPConstants.AGENCY_ID_PARAMETER));
165        userId.setAgencyId(agencyId);
166        UserIdentifierType userIdentifierType = new UserIdentifierType(oleCheckOutItem.getUserType(), oleCheckOutItem.getUserType());
167        userId.setUserIdentifierValue(initData.getUserId().getUserIdentifierValue());
168        userId.setUserIdentifierType(userIdentifierType);
169        return userId;
170    }
171
172
173}