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 org.apache.commons.lang.StringUtils;
019import org.kuali.rice.kim.api.KimConstants;
020import org.kuali.rice.kim.api.type.KimAttributeField;
021import org.kuali.rice.kim.api.type.KimType;
022import org.kuali.rice.kim.bo.impl.KimAttributes;
023import org.kuali.rice.kim.framework.services.KimFrameworkServiceLocator;
024import org.kuali.rice.kim.framework.type.KimTypeService;
025import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
026import org.kuali.rice.kns.kim.type.DataDictionaryTypeServiceHelper;
027
028import java.io.Serializable;
029import java.util.List;
030import java.util.Map;
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
040        private KimType kimType;
041        private transient KimTypeService kimTypeService;
042        private List<? extends KimAttributes> attributes;
043        private transient List<KimAttributeField> definitions;
044        private transient Map<String,Object> attributeEntry;
045
046        public KimTypeAttributesHelper(KimType kimType){
047                this.kimType = kimType;
048        }
049
050        /**
051         * @return the attributes
052         */
053        public List<? extends KimAttributes> getAttributes() {
054                return this.attributes;
055        }
056
057        /**
058         * @return the kimType
059         */
060        public KimType getKimType() {
061                return this.kimType;
062        }
063
064        public KimTypeService getKimTypeService(KimType kimType){
065                if(this.kimTypeService==null){
066                this.kimTypeService = KimFrameworkServiceLocator.getKimTypeService(kimType);
067                }
068                return this.kimTypeService;
069        }
070        
071        public Map<String,Object> getAttributeEntry() {
072                if(attributeEntry==null || attributeEntry.isEmpty())
073                        attributeEntry = KIMServiceLocatorInternal.getUiDocumentService().getAttributeEntries(getDefinitions());
074                return attributeEntry;
075        }
076
077    public String getKimAttributeDefnId(KimAttributeField definition){
078        return definition.getId();
079    }
080    
081        public List<KimAttributeField> getDefinitions() {
082                if (definitions == null || definitions.isEmpty()) {
083                KimTypeService kimTypeService = getKimTypeService(getKimType());
084                if(kimTypeService!=null)
085                        this.definitions = kimTypeService.getAttributeDefinitions(getKimType().getId());
086                }
087                return this.definitions;
088        }
089        
090        public String getCommaDelimitedAttributesLabels(String commaDelimitedAttributesNamesList){
091                String[] names = StringUtils.splitByWholeSeparator(commaDelimitedAttributesNamesList, KimConstants.KimUIConstants.COMMA_SEPARATOR);
092                StringBuffer commaDelimitedAttributesLabels = new StringBuffer();
093                for(String name: names){
094                        commaDelimitedAttributesLabels.append(getAttributeEntry().get(name.trim())+KimConstants.KimUIConstants.COMMA_SEPARATOR);
095                }
096        if(commaDelimitedAttributesLabels.toString().endsWith(KimConstants.KimUIConstants.COMMA_SEPARATOR))
097                commaDelimitedAttributesLabels.delete(commaDelimitedAttributesLabels.length()- KimConstants.KimUIConstants.COMMA_SEPARATOR.length(), commaDelimitedAttributesLabels.length());
098        return commaDelimitedAttributesLabels.toString();
099        }
100
101        public KimAttributeField getAttributeDefinition(String attributeName){
102                return DataDictionaryTypeServiceHelper.findAttributeField(attributeName, getDefinitions());
103        }
104        
105        public String getAttributeValue(Map<String, String> aSet, String attributeName){
106                if(StringUtils.isEmpty(attributeName) || aSet==null) return null;
107                for(String attributeNameKey: aSet.keySet()){
108                        if(attributeName.equals(attributeNameKey))
109                                return aSet.get(attributeNameKey);
110                }
111                return null;
112        }
113}