001package org.kuali.ole.describe.keyvalue;
002
003import org.kuali.ole.describe.form.BoundwithForm;
004import org.kuali.ole.describe.form.GlobalEditForm;
005import org.kuali.ole.describe.form.OLESearchForm;
006import org.kuali.ole.docstore.common.constants.DocstoreConstants;
007import org.kuali.ole.docstore.common.document.config.DocFieldConfig;
008import org.kuali.ole.docstore.common.document.config.DocFormatConfig;
009import org.kuali.ole.docstore.common.document.config.DocTypeConfig;
010import org.kuali.ole.docstore.common.document.config.DocumentSearchConfig;
011import org.kuali.ole.docstore.model.enums.DocType;
012import org.kuali.rice.core.api.util.ConcreteKeyValue;
013import org.kuali.rice.core.api.util.KeyValue;
014import org.kuali.rice.krad.uif.control.UifKeyValuesFinderBase;
015import org.kuali.rice.krad.uif.view.ViewModel;
016
017import java.util.ArrayList;
018import java.util.List;
019import java.util.Map;
020import java.util.TreeMap;
021
022/**
023 * Created by jayabharathreddy on 2/16/15.
024 */
025public class ItemSearchFieldKeyValuefinder extends UifKeyValuesFinderBase implements DocstoreConstants {
026
027    DocumentSearchConfig documentSearchConfig = DocumentSearchConfig.getDocumentSearchConfig();
028
029    @Override
030    public List<KeyValue> getKeyValues(ViewModel viewModel) {
031        setAddBlankOption(false);
032        OLESearchForm oleSearchForm = null;
033        GlobalEditForm globalEditForm = null;
034        BoundwithForm boundwithForm = null;
035        String docType = DocType.ITEM.getCode();
036        if (viewModel instanceof OLESearchForm) {
037            oleSearchForm = (OLESearchForm)  viewModel;
038        }
039        else if (viewModel instanceof GlobalEditForm) {
040            globalEditForm = (GlobalEditForm) viewModel;
041        }
042        else if (viewModel instanceof BoundwithForm) {
043            boundwithForm = (BoundwithForm) viewModel;
044        }
045
046        List<KeyValue> options = new ArrayList<KeyValue>();
047        Map<String, String> sortedMap = new TreeMap<>();
048        for (DocTypeConfig docTypeConfig : documentSearchConfig.getDocTypeConfigs()) {
049            if (docTypeConfig.getName().equals(docType)) {
050                for (DocFormatConfig docFormatConfig : docTypeConfig.getDocFormatConfigList()) {
051                    if (docFormatConfig.getName().equals("oleml") && DocType.ITEM.getCode().equals(docType)) {
052                        for (DocFieldConfig docFieldConfig : docFormatConfig.getDocFieldConfigList()) {
053                            if((oleSearchForm != null && docType.equalsIgnoreCase(docFieldConfig.getDocType().getName())) ||
054                                    (globalEditForm != null && globalEditForm.getDocType().equalsIgnoreCase(docFieldConfig.getDocType().getName())) ||
055                                    (boundwithForm!=null && boundwithForm.getDocType().equalsIgnoreCase(docFieldConfig.getDocType().getName()))){
056                                if(docFieldConfig.isSearchable()){
057                                    if(docFieldConfig.getName().endsWith("_search")){
058                                        sortedMap.put(docFieldConfig.getLabel(), docFieldConfig.getName());
059                                    }if(docFieldConfig.getName().equalsIgnoreCase(BIB_IDENTIFIER)){
060                                        sortedMap.put(docFieldConfig.getLabel(), docFieldConfig.getName());
061                                    }
062                                }
063                            }
064                        }
065                    }
066                }
067            }
068        }
069
070        for (String searchField : sortedMap.keySet()) {
071            options.add(new ConcreteKeyValue(sortedMap.get(searchField), searchField));
072        }
073        options.add(0,new ConcreteKeyValue("any","ANY"));
074        return options;
075    }
076}