001package org.kuali.ole.deliver.service;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.deliver.bo.OleCirculationDesk;
005import org.kuali.ole.deliver.bo.OleCirculationDeskDetail;
006import org.kuali.ole.deliver.form.OleCirculationDeskDetailForm;
007import org.kuali.rice.kim.impl.identity.principal.PrincipalBo;
008import org.kuali.rice.krad.service.BusinessObjectService;
009import org.kuali.rice.krad.service.KRADServiceLocator;
010
011import java.util.ArrayList;
012import java.util.HashMap;
013import java.util.List;
014import java.util.Map;
015
016
017/**
018 * This is the service class for performing save and search operations on Circulation Desk Mapping
019 */
020public class OleCirculationDeskDetailServiceImpl {
021
022
023    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleCirculationDeskDetailServiceImpl.class);
024    private BusinessObjectService businessObjectService;
025
026    /**
027     * Gets the instance of BusinessObjectService
028     *
029     * @return businessObjectService(BusinessObjectService)
030     */
031    public BusinessObjectService getBusinessObjectService() {
032        if (null == businessObjectService) {
033            businessObjectService = KRADServiceLocator.getBusinessObjectService();
034        }
035        return businessObjectService;
036    }
037
038
039    /**
040     * This method is for populating the circulation details based on the available circulation desk
041     *
042     * @return oleCirculationDeskDetailList
043     */
044    public List<OleCirculationDeskDetail> populateCreateList() {
045        List<OleCirculationDeskDetail> oleCirculationDeskDetailList = new ArrayList<OleCirculationDeskDetail>();
046        List<OleCirculationDesk> oleCirculationDeskList = (List<OleCirculationDesk>) getBusinessObjectService().findAll(OleCirculationDesk.class);
047        OleCirculationDeskDetail oleCirculationDeskDetail;
048        for (int i = 0; i < oleCirculationDeskList.size(); i++) {
049            if (oleCirculationDeskList.get(i).isActive()) {
050                oleCirculationDeskDetail = new OleCirculationDeskDetail();
051                oleCirculationDeskDetail.setOleCirculationDesk(oleCirculationDeskList.get(i));
052                oleCirculationDeskDetail.setCirculationDeskId(oleCirculationDeskList.get(i).getCirculationDeskId());
053                oleCirculationDeskDetailList.add(oleCirculationDeskDetail);
054            }
055        }
056        return oleCirculationDeskDetailList;
057    }
058
059    /**
060     * This method is for saving the circulation desk details for the operator
061     *
062     * @param oleCirculationDeskDetailForm
063     * @return oleCirculationDeskDetailForm
064     */
065    public OleCirculationDeskDetailForm performSave(OleCirculationDeskDetailForm oleCirculationDeskDetailForm) {
066
067        oleCirculationDeskDetailForm.setMessage(null);
068
069        String operatorId = oleCirculationDeskDetailForm.getOperatorId();
070        Map<String, String> opt = new HashMap<String, String>();
071        opt.put("principalId", operatorId);
072        List<PrincipalBo> matchedPrincipallList = (List<PrincipalBo>) getBusinessObjectService().findMatching(PrincipalBo.class, opt);
073
074        if (matchedPrincipallList.size() == 0) {
075            StringBuffer selectBuffer = new StringBuffer(OLEConstants.CRCL_DSK_INVALID_OPR);
076            oleCirculationDeskDetailForm.setMessage(selectBuffer.toString());
077            LOG.error(selectBuffer);
078            return oleCirculationDeskDetailForm;
079        } else {
080            List<OleCirculationDeskDetail> oleCirculationDeskDetails = oleCirculationDeskDetailForm.getOleCirculationDetailsCreateList();
081            int count = 0;
082            for (int k = 0; k < oleCirculationDeskDetails.size(); k++) {
083                if (oleCirculationDeskDetails.get(k).isDefaultLocation()) {
084                    count++;
085                }
086            }
087            if (count == 1 && !oleCirculationDeskDetailForm.getOperatorId().isEmpty()) {
088                List<OleCirculationDeskDetail> newOleCirculationDeskDetails = new ArrayList<OleCirculationDeskDetail>();
089
090                for (int i = 0; i < oleCirculationDeskDetails.size(); i++) {
091                    if (!oleCirculationDeskDetails.get(i).isDefaultLocation() && !oleCirculationDeskDetails.get(i).isAllowedLocation()) {
092                        getBusinessObjectService().delete(oleCirculationDeskDetails.get(i));
093                    } else {
094                        oleCirculationDeskDetails.get(i).setOperatorId(oleCirculationDeskDetailForm.getOperatorId());
095                        newOleCirculationDeskDetails.add(oleCirculationDeskDetails.get(i));
096                    }
097                }
098
099                getBusinessObjectService().save(newOleCirculationDeskDetails);
100                //Success message set for circulation desk created
101                StringBuffer newDeskDetailForm = new StringBuffer(OLEConstants.CRCL_DSK_SUCCESS);
102                oleCirculationDeskDetailForm.setMessage(newDeskDetailForm.toString());
103            } else if (oleCirculationDeskDetailForm.getOperatorId().isEmpty()) {
104                StringBuffer selectBuffer = new StringBuffer(OLEConstants.CRCL_DSK_LOC_ERR);
105                oleCirculationDeskDetailForm.setMessage(selectBuffer.toString());
106                LOG.error(selectBuffer);
107                return oleCirculationDeskDetailForm;
108            } else {
109                if (count == 0) {
110                    StringBuffer defaultBuffer = new StringBuffer(OLEConstants.CRCL_DSK_NO_LOC_ERR);
111                    oleCirculationDeskDetailForm.setMessage(defaultBuffer.toString());
112                    LOG.error(defaultBuffer);
113                    return oleCirculationDeskDetailForm;
114                }
115            }
116            oleCirculationDeskDetailForm.setOleCirculationDetailsCreateList(populateCreateList());
117            oleCirculationDeskDetailForm.setOperatorId(null);
118            return oleCirculationDeskDetailForm;
119        }
120    }
121
122
123    /**
124     * This method is for searching the circulation details
125     *
126     * @param oleCirculationDeskDetailForm
127     * @return oleCirculationDeskDetailForm
128     */
129    public OleCirculationDeskDetailForm performSearch(OleCirculationDeskDetailForm oleCirculationDeskDetailForm) {
130
131        oleCirculationDeskDetailForm.setMessage(null);
132        String operatorId = oleCirculationDeskDetailForm.getOperatorId();
133
134        Map<String, String> opt = new HashMap<String, String>();
135        opt.put("principalId", operatorId);
136        List<PrincipalBo> matchedPrincipallList = (List<PrincipalBo>) getBusinessObjectService().findMatching(PrincipalBo.class, opt);
137        if (matchedPrincipallList.size() == 0) {
138            StringBuffer selectBuffer = new StringBuffer(OLEConstants.CRCL_DSK_INVALID_OPR);
139            oleCirculationDeskDetailForm.setMessage(selectBuffer.toString());
140            LOG.error(selectBuffer);
141            return oleCirculationDeskDetailForm;
142        }
143
144        if (operatorId.isEmpty()) {
145            StringBuffer selectBuffer = new StringBuffer(OLEConstants.CRCL_DSK_LOC_ERR);
146            oleCirculationDeskDetailForm.setMessage(selectBuffer.toString());
147            LOG.error(selectBuffer);
148            return oleCirculationDeskDetailForm;
149        } else {
150            Map<String, String> detailMap = new HashMap<String, String>();
151            detailMap.put("operatorId", oleCirculationDeskDetailForm.getOperatorId());
152            List<OleCirculationDeskDetail> searchedCirculationDeskDetailList = (List<OleCirculationDeskDetail>) getBusinessObjectService().findMatching(OleCirculationDeskDetail.class, detailMap);
153            List<OleCirculationDeskDetail> allCirculationDeskDetailList = populateCreateList();
154            List<OleCirculationDeskDetail> mergedList = new ArrayList<OleCirculationDeskDetail>();
155            mergedList.addAll(searchedCirculationDeskDetailList);
156            for (int i = 0; i < searchedCirculationDeskDetailList.size(); i++) {
157                for (int j = 0; j < allCirculationDeskDetailList.size(); j++) {
158                    if (searchedCirculationDeskDetailList.get(i).getCirculationDeskId().equals(allCirculationDeskDetailList.get(j).getCirculationDeskId())) {
159                        allCirculationDeskDetailList.remove(j);
160                    }
161                }
162            }
163            mergedList.addAll(allCirculationDeskDetailList);
164            if (mergedList != null && mergedList.size() > 0) {
165                for (int i = 0; i < mergedList.size(); i++) {
166                    if (mergedList.get(i).getOleCirculationDesk() != null && !mergedList.get(i).getOleCirculationDesk().isActive()) {
167                        mergedList.remove(i);
168                    }
169                }
170            }
171            oleCirculationDeskDetailForm.setOleCirculationDetailsCreateList(mergedList);
172            return oleCirculationDeskDetailForm;
173        }
174    }
175
176}
177