<commit_before>/*
 * Copyright 2008 The Kuali Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.rice.kns.workflow.attribute;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.kuali.kfs.sys.context.SpringContext;
import org.kuali.kfs.sys.document.service.WorkflowAttributePropertyResolutionService;
import org.kuali.rice.kew.docsearch.DocumentSearchContext;
import org.kuali.rice.kew.docsearch.DocumentSearchField;
import org.kuali.rice.kew.docsearch.DocumentSearchRow;
import org.kuali.rice.kew.docsearch.SearchableAttribute;
import org.kuali.rice.kew.docsearch.SearchableAttributeValue;
import org.kuali.rice.kew.exception.WorkflowException;
import org.kuali.rice.kew.rule.WorkflowAttributeValidationError;
import org.kuali.rice.kns.bo.BusinessObject;
import org.kuali.rice.kns.datadictionary.AttributeDefinition;
import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;
import org.kuali.rice.kns.datadictionary.DocumentEntry;
import org.kuali.rice.kns.datadictionary.SearchingAttribute;
import org.kuali.rice.kns.datadictionary.SearchingTypeDefinition;
import org.kuali.rice.kns.datadictionary.WorkflowAttributes;
import org.kuali.rice.kns.document.Document;
import org.kuali.rice.kns.lookup.keyvalues.KeyValuesFinder;
import org.kuali.rice.kns.service.DataDictionaryService;
import org.kuali.rice.kns.service.DocumentService;
import org.kuali.rice.kns.web.ui.Field;

/**
 * This class...
 */
public class DataDictionarySearchableAttribute implements SearchableAttribute {

    private static final Logger LOG = Logger.getLogger(DataDictionarySearchableAttribute.class);

    
    /**
     * @see org.kuali.rice.kew.docsearch.SearchableAttribute#getSearchContent(org.kuali.rice.kew.docsearch.DocumentSearchContext)
     */
    public String getSearchContent(DocumentSearchContext documentSearchContext) {

        return "";
    }

    /**
     * @see org.kuali.rice.kew.docsearch.SearchableAttribute#getSearchStorageValues(org.kuali.rice.kew.docsearch.DocumentSearchContext)
     */
    public List<SearchableAttributeValue> getSearchStorageValues(DocumentSearchContext documentSearchContext) {
        
        String docId = documentSearchContext.getDocumentId();
        DocumentEntry docEntry = SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getDocumentEntry(documentSearchContext.getDocumentTypeName());

        DocumentService docService = SpringContext.getBean(DocumentService.class);
        Document doc = null;
        try  {
            doc = docService.getByDocumentHeaderIdSessionless(docId);
        } catch (WorkflowException we) {
            
        }
        WorkflowAttributes workflowAttributes = docEntry.getWorkflowAttributes();
        WorkflowAttributePropertyResolutionService waprs = SpringContext.getBean(WorkflowAttributePropertyResolutionService.class);
        return waprs.resolveSearchableAttributeValues(doc, workflowAttributes);
    }

    /**
     * @see org.kuali.rice.kew.docsearch.SearchableAttribute#getSearchingRows(org.kuali.rice.kew.docsearch.DocumentSearchContext)
     */
    public List<DocumentSearchRow> getSearchingRows(DocumentSearchContext documentSearchContext) {
        
       
       DocumentEntry entry = SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getDocumentEntry(documentSearchContext.getDocumentTypeName());
       WorkflowAttributes workflowAttributes = entry.getWorkflowAttributes();

       List<DocumentSearchRow> retvals = createFieldRowsForWorkflowAttributes(workflowAttributes);
        
        return retvals;
    }

    /**
     * @see org.kuali.rice.kew.docsearch.SearchableAttribute#validateUserSearchInputs(java.util.Map, org.kuali.rice.kew.docsearch.DocumentSearchContext)
     */
    public List<WorkflowAttributeValidationError> validateUserSearchInputs(Map<Object, String> paramMap, DocumentSearchContext searchContext) {

        return null;
    }

    /**
     * Creates a list of search fields, one for each primary key of the maintained business object 
     * @param businessObjectClass the class of the maintained business object
     * @return a List of KEW search fields
     */
    protected List<DocumentSearchRow> createFieldRowsForWorkflowAttributes(WorkflowAttributes attrs) {
        List<DocumentSearchRow> searchFields = new ArrayList<DocumentSearchRow>();
        
      List<SearchingTypeDefinition> searchingTypeDefinitions = attrs.getSearchingTypeDefinitions();
      for (SearchingTypeDefinition definition: searchingTypeDefinitions) {
          SearchingAttribute attr = definition.getSearchingAttribute();
          String attributeName = attr.getAttributeName();
          String businessObjectClassName = attr.getBusinessObjectClassName();
          final BusinessObjectEntry boEntry = SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getBusinessObjectEntry(businessObjectClassName);
          DocumentSearchField searchField = buildSearchField(attributeName, boEntry);
              List<Field> fieldList = new ArrayList<Field>();
              fieldList.add(searchField);
              if (searchField.isUsingDatePicker()) {
                  fieldList.add(buildDatePickerField(attributeName));
              }
              String quickfinderService = searchField.getQuickFinderClassNameImpl();
//              if (!Utilities.isEmpty(quickfinderService)) {
//                  fieldList.add(new Field("", "", Field.QUICKFINDER, false, "", "", null, quickfinderService));
//              }
              searchFields.add(new DocumentSearchRow(fieldList));
              
      }
        
        return searchFields;
    }
    
    /**
     * Builds a search field for the given property
     * @param propertyName the name of the property to search on
     * @param businessObjectEntry the business object data dictionary for the maintained business object
     * @return a search field for the given property
     */
    protected DocumentSearchField buildSearchField(String propertyName, BusinessObjectEntry businessObjectEntry) {
        AttributeDefinition propertyAttribute = getAttributeFromEntry(propertyName, businessObjectEntry);
        DocumentSearchField field = buildFieldFromEntry(propertyAttribute);

        // 4. quickfinder?
        
     // TODO: ewestfal - 12-23-2008
        // commenting out the below for now until the new document search code is in place and we've
        // implemented support for rendering links to url-based lookups in KFS
        // also commented out getQuickfinderServiceName and getWorkflowLookupServiceName
        
//        final String quickfinderServiceName = getQuickfinderServiceName(businessObjectEntry.getBusinessObjectClass(), propertyName);
//        
//        if (quickfinderServiceName != null) {
//            field.setQuickFinderClassNameImpl(quickfinderServiceName);
//            field.setHasLookupable(true);
//            field.setDefaultLookupableName(propertyName);
//        }
        return field;
    }
    

    
    /**
     * Builds a date picker to go with a field
     * @param propertyName the name of the property which is going to have a date picker associated with it
     * @return a date picker field
     */
    protected DocumentSearchField buildDatePickerField(String propertyName) {
        return new DocumentSearchField("", "", DocumentSearchField.DATEPICKER, propertyName + "_datepicker", "", null, "");
    }
    
    /**
     * Retrieves the attribute definition from the business object data dictionary entry
     * @param propertyName the property to find the attribute definition for
     * @param businessObjectEntry the business object data dictionary entry
     * @return the found AttributeDefinition
     */
    protected AttributeDefinition getAttributeFromEntry(String propertyName, BusinessObjectEntry businessObjectEntry) {
        return businessObjectEntry.getAttributeDefinition(propertyName);
    }
    
    /**
     * Builds the field based on the attribute entry
     * @param attributeDefinition the definition of the attribute
     * @return a KEW field based on the attribute definition
     */
    protected DocumentSearchField buildFieldFromEntry(AttributeDefinition attributeDefinition) {
        DocumentSearchField field = new DocumentSearchField(
                attributeDefinition.getLabel(), // label
                "", // help url
                determineKEWFieldType(attributeDefinition), // field type
                attributeDefinition.getName(), // property name
                "", // property value
                getValidValues(attributeDefinition), // field valid values
                "" // quick finder class name
                );
        calibrateField(field, attributeDefinition);
        return field;
    }
   
    /**<commit_msg>Make DDSearchableAttribute a bit more abstract on where it gets Attributes from.<commit_after>/*
 * Copyright 2008 The Kuali Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.rice.kns.workflow.attribute;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.kuali.kfs.sys.context.SpringContext;
import org.kuali.kfs.sys.document.service.WorkflowAttributePropertyResolutionService;
import org.kuali.rice.kew.docsearch.DocumentSearchContext;
import org.kuali.rice.kew.docsearch.DocumentSearchField;
import org.kuali.rice.kew.docsearch.DocumentSearchRow;
import org.kuali.rice.kew.docsearch.SearchableAttribute;
import org.kuali.rice.kew.docsearch.SearchableAttributeValue;
import org.kuali.rice.kew.exception.WorkflowException;
import org.kuali.rice.kew.rule.WorkflowAttributeValidationError;
import org.kuali.rice.kns.bo.BusinessObject;
import org.kuali.rice.kns.datadictionary.AttributeDefinition;
import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;
import org.kuali.rice.kns.datadictionary.DataDictionaryEntry;
import org.kuali.rice.kns.datadictionary.DocumentEntry;
import org.kuali.rice.kns.datadictionary.SearchingAttribute;
import org.kuali.rice.kns.datadictionary.SearchingTypeDefinition;
import org.kuali.rice.kns.datadictionary.WorkflowAttributes;
import org.kuali.rice.kns.document.Document;
import org.kuali.rice.kns.lookup.keyvalues.KeyValuesFinder;
import org.kuali.rice.kns.service.DataDictionaryService;
import org.kuali.rice.kns.service.DocumentService;
import org.kuali.rice.kns.web.ui.Field;

/**
 * This class...
 */
public class DataDictionarySearchableAttribute implements SearchableAttribute {

    private static final Logger LOG = Logger.getLogger(DataDictionarySearchableAttribute.class);

    
    /**
     * @see org.kuali.rice.kew.docsearch.SearchableAttribute#getSearchContent(org.kuali.rice.kew.docsearch.DocumentSearchContext)
     */
    public String getSearchContent(DocumentSearchContext documentSearchContext) {

        return "";
    }

    /**
     * @see org.kuali.rice.kew.docsearch.SearchableAttribute#getSearchStorageValues(org.kuali.rice.kew.docsearch.DocumentSearchContext)
     */
    public List<SearchableAttributeValue> getSearchStorageValues(DocumentSearchContext documentSearchContext) {
        
        String docId = documentSearchContext.getDocumentId();
        DocumentEntry docEntry = SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getDocumentEntry(documentSearchContext.getDocumentTypeName());

        DocumentService docService = SpringContext.getBean(DocumentService.class);
        Document doc = null;
        try  {
            doc = docService.getByDocumentHeaderIdSessionless(docId);
        } catch (WorkflowException we) {
            
        }
        WorkflowAttributes workflowAttributes = docEntry.getWorkflowAttributes();
        WorkflowAttributePropertyResolutionService waprs = SpringContext.getBean(WorkflowAttributePropertyResolutionService.class);
        return waprs.resolveSearchableAttributeValues(doc, workflowAttributes);
    }

    /**
     * @see org.kuali.rice.kew.docsearch.SearchableAttribute#getSearchingRows(org.kuali.rice.kew.docsearch.DocumentSearchContext)
     */
    public List<DocumentSearchRow> getSearchingRows(DocumentSearchContext documentSearchContext) {
        
       
       DocumentEntry entry = SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getDocumentEntry(documentSearchContext.getDocumentTypeName());
       WorkflowAttributes workflowAttributes = entry.getWorkflowAttributes();

       List<DocumentSearchRow> retvals = createFieldRowsForWorkflowAttributes(workflowAttributes);
        
        return retvals;
    }

    /**
     * @see org.kuali.rice.kew.docsearch.SearchableAttribute#validateUserSearchInputs(java.util.Map, org.kuali.rice.kew.docsearch.DocumentSearchContext)
     */
    public List<WorkflowAttributeValidationError> validateUserSearchInputs(Map<Object, String> paramMap, DocumentSearchContext searchContext) {

        return null;
    }

    /**
     * Creates a list of search fields, one for each primary key of the maintained business object 
     * @param businessObjectClass the class of the maintained business object
     * @return a List of KEW search fields
     */
    protected List<DocumentSearchRow> createFieldRowsForWorkflowAttributes(WorkflowAttributes attrs) {
        List<DocumentSearchRow> searchFields = new ArrayList<DocumentSearchRow>();
        
      List<SearchingTypeDefinition> searchingTypeDefinitions = attrs.getSearchingTypeDefinitions();
      for (SearchingTypeDefinition definition: searchingTypeDefinitions) {
          SearchingAttribute attr = definition.getSearchingAttribute();
          String attributeName = attr.getAttributeName();
          String businessObjectClassName = attr.getBusinessObjectClassName();
          final DataDictionaryEntry boEntry = SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getDictionaryObjectEntry(businessObjectClassName);
          DocumentSearchField searchField = buildSearchField(attributeName, boEntry);
              List<Field> fieldList = new ArrayList<Field>();
              fieldList.add(searchField);
              if (searchField.isUsingDatePicker()) {
                  fieldList.add(buildDatePickerField(attributeName));
              }
              String quickfinderService = searchField.getQuickFinderClassNameImpl();
//              if (!Utilities.isEmpty(quickfinderService)) {
//                  fieldList.add(new Field("", "", Field.QUICKFINDER, false, "", "", null, quickfinderService));
//              }
              searchFields.add(new DocumentSearchRow(fieldList));
              
      }
        
        return searchFields;
    }
    
    /**
     * Builds a search field for the given property
     * @param propertyName the name of the property to search on
     * @param businessObjectEntry the business object data dictionary for the maintained business object
     * @return a search field for the given property
     */
    protected DocumentSearchField buildSearchField(String propertyName, DataDictionaryEntry dataDictionaryEntry) {
        AttributeDefinition propertyAttribute = getAttributeFromEntry(propertyName, dataDictionaryEntry);
        DocumentSearchField field = buildFieldFromEntry(propertyAttribute);

        // 4. quickfinder?
        
     // TODO: ewestfal - 12-23-2008
        // commenting out the below for now until the new document search code is in place and we've
        // implemented support for rendering links to url-based lookups in KFS
        // also commented out getQuickfinderServiceName and getWorkflowLookupServiceName
        
//        final String quickfinderServiceName = getQuickfinderServiceName(businessObjectEntry.getBusinessObjectClass(), propertyName);
//        
//        if (quickfinderServiceName != null) {
//            field.setQuickFinderClassNameImpl(quickfinderServiceName);
//            field.setHasLookupable(true);
//            field.setDefaultLookupableName(propertyName);
//        }
        return field;
    }
    

    
    /**
     * Builds a date picker to go with a field
     * @param propertyName the name of the property which is going to have a date picker associated with it
     * @return a date picker field
     */
    protected DocumentSearchField buildDatePickerField(String propertyName) {
        return new DocumentSearchField("", "", DocumentSearchField.DATEPICKER, propertyName + "_datepicker", "", null, "");
    }
    
    /**
     * Retrieves the attribute definition from the business object data dictionary entry
     * @param propertyName the property to find the attribute definition for
     * @param businessObjectEntry the business object data dictionary entry
     * @return the found AttributeDefinition
     */
    protected AttributeDefinition getAttributeFromEntry(String propertyName, DataDictionaryEntry dataDictionaryEntry) {
        return dataDictionaryEntry.getAttributeDefinition(propertyName);
    }
    
    /**
     * Builds the field based on the attribute entry
     * @param attributeDefinition the definition of the attribute
     * @return a KEW field based on the attribute definition
     */
    protected DocumentSearchField buildFieldFromEntry(AttributeDefinition attributeDefinition) {
        DocumentSearchField field = new DocumentSearchField(
                attributeDefinition.getLabel(), // label
                "", // help url
                determineKEWFieldType(attributeDefinition), // field type
                attributeDefinition.getName(), // property name
                "", // property value
                getValidValues(attributeDefinition), // field valid values
                "" // quick finder class name
                );
        calibrateField(field, attributeDefinition);
        return field;
    }
   
    /**
{'fraction_non_alphanumeric': 0.07947755274707842, 'fraction_numerical': 0.0017978954047908624, 'mean_word_length': 3.5179168657429525, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3960766', 'n_tokens_mistral': 4783, 'n_tokens_neox': 4701, 'n_words': 1109}