001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kim.impl.type;
017
018import java.io.Serializable;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.commons.lang.StringUtils;
023import org.kuali.rice.kim.api.KimConstants;
024import org.kuali.rice.kim.api.type.KimAttributeField;
025import org.kuali.rice.kim.api.type.KimType;
026import org.kuali.rice.kim.bo.impl.KimAttributes;
027import org.kuali.rice.kim.framework.services.KimFrameworkServiceLocator;
028import org.kuali.rice.kim.framework.type.KimTypeService;
029import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
030import org.kuali.rice.kns.kim.type.DataDictionaryTypeServiceHelper;
031
032/**
033 * This is a description of what this class does - bhargavp don't forget to fill this in. 
034 * 
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 *
037 */
038public class KimTypeAttributesHelper implements Serializable {
039        private static final long serialVersionUID = 1L;
040
041        private KimType kimType;
042        private transient KimTypeService kimTypeService;
043        private List<? extends KimAttributes> attributes;
044        private transient List<KimAttributeField> definitions;
045        private transient Map<String,Object> attributeEntry;
046
047        public KimTypeAttributesHelper(KimType kimType){
048                this.kimType = kimType;
049        }
050
051        /**
052         * @return the attributes
053         */
054        public List<? extends KimAttributes> getAttributes() {
055                return this.attributes;
056        }
057
058        /**
059         * @return the kimType
060         */
061        public KimType getKimType() {
062                return this.kimType;
063        }
064
065        public KimTypeService getKimTypeService(KimType kimType){
066                if(this.kimTypeService==null){
067                this.kimTypeService = KimFrameworkServiceLocator.getKimTypeService(kimType);
068                }
069                return this.kimTypeService;
070        }
071        
072        public Map<String,Object> getAttributeEntry() {
073                if(attributeEntry==null || attributeEntry.isEmpty())
074                        attributeEntry = KIMServiceLocatorInternal.getUiDocumentService().getAttributeEntries(getDefinitions());
075                return attributeEntry;
076        }
077
078    public String getKimAttributeDefnId(KimAttributeField definition){
079        return definition.getId();
080    }
081    
082        public List<KimAttributeField> getDefinitions() {
083                if (definitions == null || definitions.isEmpty()) {
084                KimTypeService kimTypeService = getKimTypeService(getKimType());
085                if(kimTypeService!=null)
086                        this.definitions = kimTypeService.getAttributeDefinitions(getKimType().getId());
087                }
088                return this.definitions;
089        }
090        
091        public String getCommaDelimitedAttributesLabels(String commaDelimitedAttributesNamesList){
092                String[] names = StringUtils.splitByWholeSeparator(commaDelimitedAttributesNamesList, KimConstants.KimUIConstants.COMMA_SEPARATOR);
093                StringBuffer commaDelimitedAttributesLabels = new StringBuffer();
094                for(String name: names){
095                        commaDelimitedAttributesLabels.append(getAttributeEntry().get(name.trim())+KimConstants.KimUIConstants.COMMA_SEPARATOR);
096                }
097        if(commaDelimitedAttributesLabels.toString().endsWith(KimConstants.KimUIConstants.COMMA_SEPARATOR))
098                commaDelimitedAttributesLabels.delete(commaDelimitedAttributesLabels.length()- KimConstants.KimUIConstants.COMMA_SEPARATOR.length(), commaDelimitedAttributesLabels.length());
099        return commaDelimitedAttributesLabels.toString();
100        }
101
102        public KimAttributeField getAttributeDefinition(String attributeName){
103                return DataDictionaryTypeServiceHelper.findAttributeField(attributeName, getDefinitions());
104        }
105        
106        public String getAttributeValue(Map<String, String> aSet, String attributeName){
107                if(StringUtils.isEmpty(attributeName) || aSet==null) return null;
108                for(String attributeNameKey: aSet.keySet()){
109                        if(attributeName.equals(attributeNameKey))
110                                return aSet.get(attributeNameKey);
111                }
112                return null;
113        }
114}