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