001package org.kuali.ole.describe.keyvalue;
002
003import org.apache.commons.lang3.StringUtils;
004import org.kuali.ole.describe.form.BoundwithForm;
005import org.kuali.ole.describe.form.GlobalEditForm;
006import org.kuali.ole.describe.form.ImportBibForm;
007import org.kuali.ole.describe.form.WorkbenchForm;
008import org.kuali.ole.docstore.common.search.SearchParams;
009import org.kuali.ole.docstore.model.enums.DocCategory;
010import org.kuali.ole.docstore.model.enums.DocFormat;
011import org.kuali.ole.docstore.model.enums.DocType;
012import org.kuali.ole.docstore.model.xmlpojo.config.*;
013import org.kuali.ole.describe.service.DocstoreHelperService;
014import org.kuali.rice.core.api.util.ConcreteKeyValue;
015import org.kuali.rice.core.api.util.KeyValue;
016import org.kuali.rice.krad.uif.control.UifKeyValuesFinderBase;
017import org.kuali.rice.krad.uif.view.ViewModel;
018import org.slf4j.Logger;
019import org.slf4j.LoggerFactory;
020
021import java.util.ArrayList;
022import java.util.List;
023
024/**
025 * Created with IntelliJ IDEA.
026 * User: PP7788
027 * Date: 11/29/12
028 * Time: 5:02 PM
029 * To change this template use File | Settings | File Templates.
030 */
031public class DocFieldKeyValueFinder
032        extends UifKeyValuesFinderBase {
033
034    public static final Logger LOG = LoggerFactory.getLogger(DocFieldKeyValueFinder.class);
035
036    @Override
037    public List<KeyValue> getKeyValues(ViewModel viewModel) {
038        List<KeyValue> options = new ArrayList<KeyValue>();
039        SearchParams searchParams = null;
040        if (viewModel instanceof WorkbenchForm) {
041            WorkbenchForm workbenchForm = (WorkbenchForm) viewModel;
042            searchParams = workbenchForm.getSearchParams();
043            getOptions(workbenchForm.getDocType(), options);
044        } else if (viewModel instanceof BoundwithForm) {
045            BoundwithForm boundwithForm = (BoundwithForm) viewModel;
046            searchParams = boundwithForm.getSearchParams();
047            getOptions(boundwithForm.getDocType(), options);
048        } else if (viewModel instanceof ImportBibForm) {
049            ImportBibForm importBibForm = (ImportBibForm) viewModel;
050            getOptionsImportBib(importBibForm, options);
051        } else if (viewModel instanceof GlobalEditForm) {
052            GlobalEditForm globalEditForm = (GlobalEditForm) viewModel;
053            /*if((globalEditForm.getSearchType() != null &&!globalEditForm.getSearchType().equalsIgnoreCase("import")) &&
054                    (globalEditForm.getGlobalEditRecords().size() > 0 && !globalEditForm.getDocType().equals(globalEditForm.getGlobalEditRecords().get(0).getDocType()))) {
055                globalEditForm.getGlobalEditRecords().clear();
056                globalEditForm.getGlobalEditMap().clear();
057            }*/
058            getOptions(globalEditForm.getDocType(), options);
059        }
060        return options;
061    }
062
063    private void getOptionsImportBib(ImportBibForm importBibForm, List<KeyValue> options) {
064        //        if (importBibForm.getSearchParams().getDocType().equalsIgnoreCase(OLEConstants.BIB_DOC_TYPE)) {
065        options.add(new ConcreteKeyValue("Title", "Title"));
066        options.add(new ConcreteKeyValue("OCLCControlNumber", "OCLC Control Number"));
067        options.add(new ConcreteKeyValue("Author", "Author"));
068        options.add(new ConcreteKeyValue("ISBN", "ISBN"));
069        options.add(new ConcreteKeyValue("ISSN", "ISSN"));
070        options.add(new ConcreteKeyValue("LCCN", "LCCN"));
071    }
072
073    private void getOptions(String docType, List<KeyValue> options) {
074        DocstoreHelperService docstoreHelperService = new DocstoreHelperService();
075        DocumentConfig documentConfig = docstoreHelperService.getDocumentConfigObj();
076        try {
077            for (DocumentCategory documentCategory : documentConfig.getDocumentCategories()) {
078                if (DocCategory.WORK.getDescription().equalsIgnoreCase(documentCategory.getName())) {
079                    for (DocumentType documentType : documentCategory.getDocumentTypes()) {
080                        if (StringUtils.isNotEmpty(docType) && docType.equalsIgnoreCase(documentType.getId())) {
081                            if (DocType.BIB.getDescription().equalsIgnoreCase(documentType.getId())) {
082                                addFieldsToOptions(documentType, options, "all");
083                            } else {
084                                addFieldsToOptions(documentType, options, DocFormat.OLEML.getDescription());
085                            }
086                        }
087                    }
088                }
089            }
090        } catch (Exception e) {
091            LOG.error(e.getMessage(), e);
092        }
093    }
094
095    private void addFieldsToOptions(DocumentType documentType, List<KeyValue> options, String docFormat) {
096        options.add(new ConcreteKeyValue("all", "ALL"));
097        for (DocumentFormat documentFormat : documentType.getDocumentFormats()) {
098            if (docFormat.equalsIgnoreCase(documentFormat.getId())) {
099                for (Field field : documentFormat.getFields()) {
100                    if ((field.getId()).endsWith("_search")) {
101                        options.add(new ConcreteKeyValue(field.getId(), field.getName()));
102                    }
103                }
104            }
105        }
106    }
107
108
109}