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.document.config.DocFieldConfig;
007import org.kuali.ole.docstore.common.document.config.DocFormatConfig;
008import org.kuali.ole.docstore.common.document.config.DocTypeConfig;
009import org.kuali.ole.docstore.common.document.config.DocumentSearchConfig;
010import org.kuali.ole.docstore.engine.service.index.solr.ItemConstants;
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.service.KRADServiceLocator;
015import org.kuali.rice.krad.uif.control.UifKeyValuesFinderBase;
016import org.kuali.rice.krad.uif.view.ViewModel;
017
018import java.util.*;
019
020/**
021 * Created with IntelliJ IDEA.
022 * User: srirams
023 * Date: 3/12/14
024 * Time: 6:02 PM
025 * To change this template use File | Settings | File Templates.
026 */
027public class SearchFieldKeyValuefinder extends UifKeyValuesFinderBase {
028
029    DocumentSearchConfig documentSearchConfig = DocumentSearchConfig.getDocumentSearchConfig();
030
031    @Override
032    public List<KeyValue> getKeyValues(ViewModel viewModel) {
033        setAddBlankOption(false);
034        OLESearchForm oleSearchForm = null;
035        GlobalEditForm globalEditForm = null;
036        BoundwithForm boundwithForm = null;
037        String docType = null;
038        if (viewModel instanceof OLESearchForm) {
039            oleSearchForm = (OLESearchForm)  viewModel;
040            docType = oleSearchForm.getDocType();
041        }
042        else if (viewModel instanceof GlobalEditForm) {
043            globalEditForm = (GlobalEditForm) viewModel;
044            docType = globalEditForm.getDocType();
045        }
046        else if (viewModel instanceof BoundwithForm) {
047            boundwithForm = (BoundwithForm) viewModel;
048            docType = boundwithForm.getDocType();
049        }
050        List<KeyValue> options = new ArrayList<KeyValue>();
051        Map<String, String> sortedMap = new TreeMap<>();
052        for (DocTypeConfig docTypeConfig : documentSearchConfig.getDocTypeConfigs()) {
053            if (docTypeConfig.getName().equals(docType)) {
054                for (DocFormatConfig docFormatConfig : docTypeConfig.getDocFormatConfigList()) {
055                    if (docFormatConfig.getName().equals("marc") && DocType.BIB.getCode().equals(docType)){
056                        for (DocFieldConfig docFieldConfig : docFormatConfig.getDocFieldConfigList()) {
057                            if((oleSearchForm != null && docType.equalsIgnoreCase(docFieldConfig.getDocType().getName())) ||
058                                    (globalEditForm != null && globalEditForm.getDocType().equalsIgnoreCase(docFieldConfig.getDocType().getName())) ||
059                                    (boundwithForm != null && boundwithForm.getDocType().equalsIgnoreCase(docFieldConfig.getDocType().getName()))){
060                                if(docFieldConfig.isSearchable()){
061                                    if (docFieldConfig.getName().endsWith("_search")) {
062                                        sortedMap.put(docFieldConfig.getLabel(), docFieldConfig.getName());
063                                    } else if (docFieldConfig.getName().equalsIgnoreCase("mdf_035a")) {
064                                        sortedMap.put(docFieldConfig.getLabel(), docFieldConfig.getName());
065                                    }
066                                }
067                            }
068
069                        }
070                    } else if (docFormatConfig.getName().equals("oleml") && !DocType.BIB.getCode().equals(docType)) {
071                        for (DocFieldConfig docFieldConfig : docFormatConfig.getDocFieldConfigList()) {
072                            if((oleSearchForm != null && docType.equalsIgnoreCase(docFieldConfig.getDocType().getName())) ||
073                                    (globalEditForm != null && globalEditForm.getDocType().equalsIgnoreCase(docFieldConfig.getDocType().getName())) ||
074                                    (boundwithForm!=null && boundwithForm.getDocType().equalsIgnoreCase(docFieldConfig.getDocType().getName()))){
075                                        if(docFieldConfig.isSearchable()){
076                                            if(docFieldConfig.getName().endsWith("_search")){
077                                                sortedMap.put(docFieldConfig.getLabel(), docFieldConfig.getName());
078                                            }if(docFieldConfig.getName().equalsIgnoreCase(ItemConstants.BIB_IDENTIFIER)){
079                                                sortedMap.put(docFieldConfig.getLabel(), docFieldConfig.getName());
080                                            }
081                                        }
082                            }
083                        }
084                    }
085                }
086            }
087        }
088
089        for (String searchField : sortedMap.keySet()) {
090            options.add(new ConcreteKeyValue(sortedMap.get(searchField), searchField));
091        }
092        options.add(0,new ConcreteKeyValue("any","ANY"));
093        return options;
094    }
095}