001/**
002 * Copyright 2005-2017 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.util.ArrayList;
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import javax.persistence.Transient;
025import javax.xml.ws.WebServiceException;
026
027import org.apache.commons.lang.StringUtils;
028import org.kuali.rice.kim.api.KimConstants;
029import org.kuali.rice.kim.api.type.KimAttributeField;
030import org.kuali.rice.kim.api.type.KimType;
031import org.kuali.rice.kim.bo.impl.KimAttributes;
032import org.kuali.rice.kim.document.IdentityManagementKimDocument;
033import org.kuali.rice.kim.framework.services.KimFrameworkServiceLocator;
034import org.kuali.rice.kim.framework.type.KimTypeService;
035import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
036import org.kuali.rice.krad.util.GlobalVariables;
037
038/**
039 * This is a description of what this class does - shyu don't forget to fill
040 * this in.
041 * 
042 * @author Kuali Rice Team (rice.collab@kuali.org)
043 * 
044 */
045public class IdentityManagementTypeAttributeTransactionalDocument extends IdentityManagementKimDocument {
046
047        protected static final long serialVersionUID = -9064436454008712125L;
048        @Transient
049        protected transient KimTypeService kimTypeService;
050        @Transient
051        protected KimType kimType;
052        @Transient
053        protected List<? extends KimAttributes> attributes;
054        @Transient
055        protected transient List<KimAttributeField> definitions;
056        @Transient
057        protected transient Map<String,Object> attributeEntry;
058        
059        /**
060         * @return the attributes
061         */
062        public List<? extends KimAttributes> getAttributes() {
063                return this.attributes;
064        }
065        /**
066         * @param attributes the attributes to set
067         */
068        public void setAttributes(List<? extends KimAttributes> attributes) {
069                this.attributes = attributes;
070        }
071        /**
072         * @return the kimType
073         */
074        public KimType getKimType() {
075                return this.kimType;
076        }
077        /**
078         * @param kimType the kimType to set
079         */
080        public void setKimType(KimType kimType) {
081                this.kimType = kimType;
082        }
083
084        public Map<String,Object> getAttributeEntry() {
085                if(attributeEntry==null || attributeEntry.isEmpty())
086                        attributeEntry = KIMServiceLocatorInternal.getUiDocumentService().getAttributeEntries(getDefinitions());
087                return attributeEntry;
088        }
089
090        public String getCommaDelimitedAttributesLabels(String commaDelimitedAttributesNamesList){
091                String[] names = StringUtils.splitByWholeSeparator(commaDelimitedAttributesNamesList, KimConstants.KimUIConstants.COMMA_SEPARATOR);
092                List<String> commaDelimitedAttributesLabels = new ArrayList<String>(names.length);
093                for(String name: names){
094                    Object attributeEntry = getAttributeEntry().get(name.trim());
095                    if ( attributeEntry != null ) {
096                        commaDelimitedAttributesLabels.add( attributeEntry.toString() );
097                    }
098                }
099        return StringUtils.join(commaDelimitedAttributesLabels, KimConstants.KimUIConstants.COMMA_SEPARATOR);
100        }
101
102        public List<KimAttributeField> getDefinitions() {
103                if (this.definitions == null || this.definitions.isEmpty()) {
104            this.definitions = Collections.emptyList();
105            KimTypeService kimTypeService = getKimTypeService(getKimType());
106                if(kimTypeService!=null) {
107                try {
108                          this.definitions = kimTypeService.getAttributeDefinitions(getKimType().getId());
109                } catch (WebServiceException e) {
110                    GlobalVariables.getMessageMap().putWarning("document.qualifier", "error.document.maintenance.group.remoteAttributesNotAvailable");
111                    LOG.warn("Not able to retrieve attribute definitions via KimTypeservice from remote system for KIM Type: " + kimType.getName(), e);
112                }
113            }
114                }
115                return this.definitions;
116        }
117
118    public Map<String, KimAttributeField> getDefinitionsKeyedByAttributeName() {
119        final Map<String, KimAttributeField> map = new HashMap<String, KimAttributeField>();
120        for (KimAttributeField field : getDefinitions()) {
121            map.put(field.getAttributeField().getName(), field);
122        }
123        return map;
124        }
125
126
127        public void setDefinitions(List<KimAttributeField> definitions) {
128                this.definitions = definitions;
129        }
130
131        protected KimTypeService getKimTypeService(KimType kimType){
132                if( kimTypeService==null){
133                kimTypeService = KimFrameworkServiceLocator.getKimTypeService(kimType);
134                }
135                return kimTypeService;
136        }
137
138}