001package org.kuali.ole.service.impl;
002
003import org.kuali.ole.DataCarrierService;
004import org.kuali.ole.OLEConstants;
005import org.kuali.ole.docstore.common.document.content.bib.marc.DataField;
006import org.kuali.ole.docstore.common.document.content.bib.marc.SubField;
007import org.kuali.ole.ingest.pojo.OleDataField;
008import org.kuali.ole.ingest.pojo.OverlayOption;
009import org.kuali.ole.select.bo.*;
010import org.kuali.ole.service.OverlayRetrivalService;
011import org.kuali.ole.util.StringUtil;
012import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
013import org.kuali.rice.krad.service.BusinessObjectService;
014import org.kuali.rice.krad.service.KRADServiceLocator;
015
016import java.util.ArrayList;
017import java.util.HashMap;
018import java.util.List;
019import java.util.Map;
020
021/**
022 * Created with IntelliJ IDEA.
023 * User: ?
024 * Date: 12/10/12
025 * Time: 6:24 PM
026 * To change this template use File | Settings | File Templates.
027 */
028
029public class OverlayRetrivalServiceImpl implements OverlayRetrivalService {
030
031    private DataCarrierService dataCarrierService;
032
033    @Override
034    public List<OleGloballyProtectedField> getGloballyProtectedFields() throws Exception {
035        BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
036        List<OleGloballyProtectedField> oleGloballyProtectedFieldList = (List<OleGloballyProtectedField>) businessObjectService.findAll(OleGloballyProtectedField.class);
037        return oleGloballyProtectedFieldList;
038    }
039
040    @Override
041    public List<String> getGloballyProtectedFieldsList()throws Exception{
042        List fieldList = new ArrayList<String>();
043        StringBuffer fields = null;
044        String tagField = null;
045        String firstIndicator = null;
046        String secondIndicator = null;
047        String subField = null;
048        List<OleGloballyProtectedField> protectedFieldList = getGloballyProtectedFields();
049        for(OleGloballyProtectedField oleGloballyProtectedField : protectedFieldList){
050            tagField = oleGloballyProtectedField.getTag();
051            firstIndicator = oleGloballyProtectedField.getFirstIndicator()!=null? StringUtil.trimHashNullValues(oleGloballyProtectedField.getFirstIndicator()):OLEConstants.DELIMITER_HASH;
052            secondIndicator = oleGloballyProtectedField.getSecondIndicator()!=null? StringUtil.trimHashNullValues(oleGloballyProtectedField.getSecondIndicator()):OLEConstants.DELIMITER_HASH;
053            subField = oleGloballyProtectedField.getSubField()!=null?OLEConstants.DELIMITER_DOLLAR+oleGloballyProtectedField.getSubField():OLEConstants.DELIMITER_DOLLAR+"*";
054            fields = new StringBuffer();
055            fields = fields.append(tagField).append(firstIndicator).append(secondIndicator).append(subField);
056            fieldList.add(fields.toString());
057        }
058
059        return fieldList;
060    }
061
062    @Override
063    public List getGloballyProtectedFieldsModificationList() throws Exception {
064        List fieldList = new ArrayList<String>();
065        StringBuffer fields = null;
066        String tagField = null;
067        String firstIndicator = null;
068        String secondIndicator = null;
069        String subField = null;
070        Map<String, String> criteria = new HashMap<String, String>();
071        criteria.put("modifyFlag","false");
072        BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
073        List<OleGloballyProtectedField> protectedFieldList = (List<OleGloballyProtectedField>)businessObjectService.findMatching(OleGloballyProtectedField.class,criteria);
074        for(OleGloballyProtectedField oleGloballyProtectedField : protectedFieldList){
075            //if(!oleGloballyProtectedField.isModifyFlag()){
076                tagField = oleGloballyProtectedField.getTag();
077                firstIndicator = oleGloballyProtectedField.getFirstIndicator()!=null?"-"+ oleGloballyProtectedField.getFirstIndicator():"-";
078                secondIndicator = oleGloballyProtectedField.getSecondIndicator()!=null?"-"+ oleGloballyProtectedField.getSecondIndicator():"-";
079                subField = oleGloballyProtectedField.getSubField()!=null?"-"+ oleGloballyProtectedField.getSubField():"-";
080                fields = new StringBuffer();
081                fields = fields.append(tagField).append(firstIndicator).append(secondIndicator).append(subField);
082                fieldList.add(fields.toString());
083            //}
084        }
085
086        return fieldList;
087    }
088
089    @Override
090    public OverlayOption getAddOverlayOptionWithWildCardSearch(List<DataField> newDatafields, List<OverlayOption> overlayOptionList) throws Exception {
091        OverlayOption addOverlayOption = null;
092        for(OverlayOption overlayOption : overlayOptionList){
093            if(overlayOption.getName().equals(OLEConstants.OVERLAY_OPTION_ADD)){
094                overlayOption = checkWildCard(newDatafields, overlayOption);
095                addOverlayOption = overlayOption;
096            }
097        }
098        return addOverlayOption;
099    }
100
101    @Override
102    public OverlayOption getAddOverlayOption(List<OverlayOption> overlayOptionList)throws Exception{
103        OverlayOption addOverlayOption = null;
104        for(OverlayOption overlayOption : overlayOptionList){
105            if(overlayOption.getName().equals(OLEConstants.OVERLAY_OPTION_ADD)){
106                addOverlayOption = overlayOption;
107            }
108        }
109        return addOverlayOption;
110    }
111
112    @Override
113    public OverlayOption getDeleteOverlayOption(List<OverlayOption> overlayOptionList)throws Exception{
114        OverlayOption deleteOverlayOption = null;
115        for(OverlayOption overlayOption : overlayOptionList){
116            if(overlayOption.getName().equals(OLEConstants.OVERLAY_OPTION_DELETE)){
117                deleteOverlayOption = overlayOption;
118            }
119        }
120        return deleteOverlayOption;
121    }
122
123    @Override
124    public OverlayOption getDeleteOverlayOptionWithWildCardSearch(List<DataField> newDatafields, List<OverlayOption> overlayOptionList) {
125        OverlayOption deleteOverlayOption = null;
126        for(OverlayOption overlayOption : overlayOptionList){
127            if(overlayOption.getName().equals(OLEConstants.OVERLAY_OPTION_DELETE)){
128                overlayOption = checkWildCard(newDatafields, overlayOption);
129                deleteOverlayOption = overlayOption;
130            }
131        }
132        return deleteOverlayOption;
133    }
134
135    @Override
136    public OverlayOption getUpdateOverlayOption(List<OverlayOption> overlayOptionList)throws Exception{
137        OverlayOption updateOverlayOption = null;
138        for(OverlayOption overlayOption : overlayOptionList){
139            if(overlayOption.getName().equals(OLEConstants.OVERLAY_OPTION_UPDATE)){
140                updateOverlayOption = overlayOption;
141            }
142        }
143        return updateOverlayOption;
144    }
145
146    @Override
147    public OverlayOption getUpdateOverlayOptionWithWildCardSearch(List<DataField> newDatafields, List<OverlayOption> overlayOptionList) throws Exception {
148        OverlayOption updateOverlayOption = null;
149        for(OverlayOption overlayOption : overlayOptionList){
150            if(overlayOption.getName().equals(OLEConstants.OVERLAY_OPTION_UPDATE)){
151                overlayOption = checkWildCard(newDatafields, overlayOption);
152                updateOverlayOption = overlayOption;
153            }
154        }
155        return updateOverlayOption;
156    }
157
158    /*@Override
159    public OverlayOption getDontAddOverlayOption(List<OverlayOption> overlayOptionList)throws Exception{
160        OverlayOption dontAddOverlayOption = null;
161        for(OverlayOption overlayOption : overlayOptionList){
162            if(overlayOption.getName().equals(OLEConstants.OVERLAY_OPTION_DONTADD)){
163                dontAddOverlayOption = overlayOption;
164            }
165        }
166        return dontAddOverlayOption;
167    }
168
169    @Override
170    public OverlayOption getDontAddOverlayOptionWithWildCardSearch(List<DataField> newDatafields, List<OverlayOption> overlayOptionList) {
171        OverlayOption dontAddOverlayOption = null;
172        for(OverlayOption overlayOption : overlayOptionList){
173            if(overlayOption.getName().equals(OLEConstants.OVERLAY_OPTION_DONTADD)){
174                overlayOption = checkWildCard(newDatafields, overlayOption);
175                dontAddOverlayOption = overlayOption;
176            }
177        }
178        return dontAddOverlayOption;
179    }*/
180
181    /*@Override
182    public String getReceiptStatus() {
183        List<ProfileAttributeBo> profileAttributesList = (List<ProfileAttributeBo>) getDataCarrierService().getData(OLEConstants.PROFILE_ATTRIBUTE_LIST);
184        String receiptStatus = new String();
185        String receiptStatusName = new String();
186        for(ProfileAttributeBo profileAttributeBo : profileAttributesList){
187            if(profileAttributeBo.getAttributeName().equals(OLEConstants.RECEIPT_STATUS)){
188                receiptStatus = profileAttributeBo.getAttributeValue();
189            }
190        }
191        BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
192        Map<String, String> attributeMap = new HashMap<String, String>();
193        attributeMap.put("receiptStatusCode",receiptStatus);
194        List<OleReceiptStatus> oleReceiptStatusList = (List<OleReceiptStatus>) businessObjectService.findMatching(OleReceiptStatus.class,attributeMap);
195        for(OleReceiptStatus oleReceiptStatus : oleReceiptStatusList){
196            receiptStatusName = oleReceiptStatus.getReceiptStatusName();
197        }
198        return receiptStatusName;
199    }*/
200
201
202    public OverlayOption checkWildCard(List<DataField> newDatafieldList, OverlayOption overlayOption){
203        List<OleDataField> overlayOptionOleDataFields = overlayOption.getOleDataFields();
204        OverlayOption newOverlayOption = new OverlayOption();
205        newOverlayOption.setId(overlayOption.getId());
206        newOverlayOption.setAgendaName(overlayOption.getAgendaName());
207        newOverlayOption.setName(overlayOption.getName());
208        List<OleDataField> oleDataFields = new ArrayList<OleDataField>();
209        for(OleDataField oleDataField : overlayOptionOleDataFields){
210            String tag = oleDataField.getDataFieldTag();
211            boolean isSubFieldCodeStar = "*".equalsIgnoreCase(oleDataField.getSubFieldCode());
212            if(tag.contains("*")){
213                char[] dataFieldSplit = oleDataField.getDataFieldTag().toCharArray();
214                for(DataField dataFields : newDatafieldList){
215                    boolean isStartsWithDataField = dataFields.getTag().startsWith(String.valueOf(dataFieldSplit[0]));
216                    if(isStartsWithDataField){
217                        for(SubField subField : dataFields.getSubFields()){
218                            if(isSubFieldCodeStar){
219                                OleDataField newOleDataFieldSubFieldCodeStar = new OleDataField();
220                                newOleDataFieldSubFieldCodeStar.setId(oleDataField.getId());
221                                newOleDataFieldSubFieldCodeStar.setOverlayOptionId(oleDataField.getOverlayOptionId());
222                                newOleDataFieldSubFieldCodeStar.setAgendaName(oleDataField.getAgendaName());
223                                newOleDataFieldSubFieldCodeStar.setDataFieldTag(dataFields.getTag());
224                                newOleDataFieldSubFieldCodeStar.setDataFieldInd1(oleDataField.getDataFieldInd1());
225                                newOleDataFieldSubFieldCodeStar.setDataFieldInd2(oleDataField.getDataFieldInd2());
226                                newOleDataFieldSubFieldCodeStar.setSubFieldCode(subField.getCode());
227                                oleDataFields.add(newOleDataFieldSubFieldCodeStar);
228                            }
229                            else if(oleDataField.getSubFieldCode().equalsIgnoreCase(subField.getCode())){
230                                OleDataField newOleDataField = new OleDataField();
231                                newOleDataField.setId(oleDataField.getId());
232                                newOleDataField.setOverlayOptionId(oleDataField.getOverlayOptionId());
233                                newOleDataField.setAgendaName(oleDataField.getAgendaName());
234                                newOleDataField.setDataFieldTag(dataFields.getTag());
235                                newOleDataField.setDataFieldInd1(oleDataField.getDataFieldInd1());
236                                newOleDataField.setDataFieldInd2(oleDataField.getDataFieldInd2());
237                                newOleDataField.setSubFieldCode(subField.getCode());
238                                oleDataFields.add(newOleDataField);
239                            }
240                        }
241                    }
242                }
243            }
244            else if(!tag.contains("*") && oleDataField.getSubFieldCode().equalsIgnoreCase("*")){
245                for(DataField dataFields : newDatafieldList){
246                    if(tag.equalsIgnoreCase(dataFields.getTag())){
247                        for(SubField subField : dataFields.getSubFields()){
248                            OleDataField newOleDataFieldSubFieldCodeStar = new OleDataField();
249                            newOleDataFieldSubFieldCodeStar.setId(oleDataField.getId());
250                            newOleDataFieldSubFieldCodeStar.setOverlayOptionId(oleDataField.getOverlayOptionId());
251                            newOleDataFieldSubFieldCodeStar.setAgendaName(oleDataField.getAgendaName());
252                            newOleDataFieldSubFieldCodeStar.setDataFieldTag(dataFields.getTag());
253                            newOleDataFieldSubFieldCodeStar.setDataFieldInd1(oleDataField.getDataFieldInd1());
254                            newOleDataFieldSubFieldCodeStar.setDataFieldInd2(oleDataField.getDataFieldInd2());
255                            newOleDataFieldSubFieldCodeStar.setSubFieldCode(subField.getCode());
256                            oleDataFields.add(newOleDataFieldSubFieldCodeStar);
257                        }
258                    }
259                }
260            }
261            else {
262                oleDataFields.add(oleDataField);
263            }
264        }
265        newOverlayOption.setOleDataFields(oleDataFields);
266        return newOverlayOption;
267    }
268    @Override
269    public OleCallNumber getCallNumberRecord(String inputValue)throws Exception{
270        BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
271        Map<String,String> criteriaMap = new HashMap<String,String>();
272        criteriaMap.put(OLEConstants.OVERLAY_INPUTVALUE,inputValue);
273        List<OleCallNumber> oleCallNumberList = (List<OleCallNumber>) businessObjectService.findMatching(OleCallNumber.class,criteriaMap);
274        if(oleCallNumberList!=null && oleCallNumberList.size()>0){
275            return oleCallNumberList.iterator().next();
276        }else{
277            return null;
278        }
279    }
280
281    public OleCallNumber getCallNumberRecord(HashMap<String,String> criteriaMap)throws Exception{
282        BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
283        List<OleCallNumber> oleCallNumberList = (List<OleCallNumber>) businessObjectService.findMatching(OleCallNumber.class,criteriaMap);
284        if(oleCallNumberList!=null && oleCallNumberList.size()>0){
285            return oleCallNumberList.iterator().next();
286        }else{
287            return null;
288        }
289    }
290    @Override
291    public OleCode getOleCodeRecord(String inputValue)throws Exception{
292        BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
293        Map<String,String> criteriaMap = new HashMap<String,String>();
294        criteriaMap.put(OLEConstants.OVERLAY_INPUTVALUE,inputValue);
295        List<OleCode> oleCodeList = (List<OleCode>) businessObjectService.findMatching(OleCode.class,criteriaMap);
296        if(oleCodeList!=null && oleCodeList.size()>0){
297            return oleCodeList.iterator().next();
298        }else{
299            return null;
300        }
301    }
302    @Override
303    public OleCode getOleCodeRecord(HashMap<String,String> criteriaMap)throws Exception{
304        BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
305        List<OleCode> oleCodeList = (List<OleCode>) businessObjectService.findMatching(OleCode.class,criteriaMap);
306        if(oleCodeList!=null && oleCodeList.size()>0){
307            return oleCodeList.iterator().next();
308        }else{
309            return null;
310        }
311    }
312    @Override
313    public OleBudgetCode getOleBudgetCode(String inputValue)throws Exception{
314        BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
315        Map<String,String> criteriaMap = new HashMap<String,String>();
316        criteriaMap.put(OLEConstants.OVERLAY_INPUTVALUE,inputValue);
317        List<OleBudgetCode> oleBudgetCodeList = (List<OleBudgetCode>) businessObjectService.findMatching(OleBudgetCode.class,criteriaMap);
318        if(oleBudgetCodeList!=null && oleBudgetCodeList.size()>0){
319            return oleBudgetCodeList.iterator().next();
320        }else{
321            return null;
322        }
323    }
324    @Override
325    public OleBudgetCode getOleBudgetCode(HashMap<String,String> criteriaMap)throws Exception{
326        BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
327        List<OleBudgetCode> oleBudgetCodeList = (List<OleBudgetCode>) businessObjectService.findMatching(OleBudgetCode.class,criteriaMap);
328        if(oleBudgetCodeList!=null && oleBudgetCodeList.size()>0){
329            return oleBudgetCodeList.iterator().next();
330        }else{
331            return null;
332        }
333    }
334
335    @Override
336    public OleVendorAccountInfo getAccountObjectForVendorRefNo(HashMap<String,String> criteriaMap)throws Exception{
337        BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
338        List<OleVendorAccountInfo> oleVendorAccountInfoList = (List<OleVendorAccountInfo>) businessObjectService.findMatching(OleVendorAccountInfo.class,criteriaMap);
339        if(oleVendorAccountInfoList!=null && oleVendorAccountInfoList.size()>0){
340            return oleVendorAccountInfoList.iterator().next();
341        }else{
342            return null;
343        }
344/*        OleVendorAccountInfo vendor=new OleVendorAccountInfo();
345        vendor=businessObjectService.findBySinglePrimaryKey(OleVendorAccountInfo.class,vendorReferenceNo);*/
346
347    }
348
349    /**
350     *  Gets the dataCarrierService attribute.
351     * @return  Returns dataCarrierService.
352     */
353    protected DataCarrierService getDataCarrierService() {
354        if(dataCarrierService == null){
355            return GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
356        }
357        return dataCarrierService;
358    }
359
360}