001 /**
002 * Copyright 2005-2012 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 */
016 package org.kuali.rice.kim.impl.common.attribute;
017
018 import javax.persistence.Transient;
019
020 import org.apache.commons.collections.CollectionUtils;
021 import org.apache.commons.lang.StringUtils;
022 import org.kuali.rice.kim.api.common.attribute.KimAttributeDataContract;
023 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
024 import org.kuali.rice.kim.api.type.KimTypeAttribute;
025 import org.kuali.rice.kim.impl.type.KimTypeBo;
026 import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
027 import org.kuali.rice.krad.util.ObjectUtils;
028
029 import java.util.ArrayList;
030 import java.util.Collection;
031 import java.util.HashMap;
032 import java.util.List;
033 import java.util.Map;
034
035 public abstract class KimAttributeDataBo extends PersistableBusinessObjectBase implements KimAttributeDataContract {
036 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KimAttributeDataBo.class);
037 private static final long serialVersionUID = 1L;
038
039 private String id;
040 private String attributeValue;
041 private String kimAttributeId;
042 private KimAttributeBo kimAttribute;
043 private String kimTypeId;
044 @Transient
045 private KimTypeBo kimType;
046
047 public abstract void setAssignedToId(String s);
048
049 @Override
050 public KimAttributeBo getKimAttribute() {
051 if(ObjectUtils.isNull(this.kimAttribute)
052 && StringUtils.isNotBlank(kimAttributeId)) {
053 this.refreshReferenceObject("kimAttribute");
054 }
055 return kimAttribute;
056 }
057
058 @Override
059 public KimTypeBo getKimType() {
060 if (kimType == null && StringUtils.isNotEmpty(id)) {
061 kimType = KimTypeBo.from(KimApiServiceLocator.getKimTypeInfoService().getKimType(kimTypeId));
062 }
063 return kimType;
064 }
065
066 public static <T extends KimAttributeDataBo> Map<String, String> toAttributes(Collection<T> bos) {
067 Map<String, String> m = new HashMap<String, String>();
068 if(CollectionUtils.isNotEmpty(bos)) {
069 for (T it : bos) {
070 if (it != null) {
071 KimTypeAttribute attribute = null;
072 if ( it.kimType != null ) {
073 attribute = KimTypeBo.to(it.kimType).getAttributeDefinitionById(it.getKimAttributeId());
074 }
075 if ( attribute != null ) {
076 m.put(attribute.getKimAttribute().getAttributeName(), it.getAttributeValue());
077 } else {
078 m.put(it.getKimAttribute().getAttributeName(), it.getAttributeValue());
079 }
080 }
081 }
082 }
083 return m;
084 }
085
086 /** creates a list of KimAttributeDataBos from attributes, kimTypeId, and assignedToId. */
087 public static <T extends KimAttributeDataBo> List<T> createFrom(Class<T> type, Map<String, String> attributes, String kimTypeId) {
088 if (attributes == null) {
089 //purposely not using Collections.emptyList() b/c we do not want to return an unmodifiable list.
090 return new ArrayList<T>();
091 }
092 List<T> attrs = new ArrayList<T>();
093 for (Map.Entry<String, String> it : attributes.entrySet()) {
094 //return attributes.entrySet().collect {
095 KimTypeAttribute attr = KimApiServiceLocator.getKimTypeInfoService().getKimType(kimTypeId).getAttributeDefinitionByName(it.getKey());
096 if (attr != null && StringUtils.isNotBlank(it.getValue())) {
097 try {
098 T newDetail = type.newInstance();
099 newDetail.setKimAttributeId(attr.getKimAttribute().getId());
100 newDetail.setKimAttribute(KimAttributeBo.from(attr.getKimAttribute()));
101 newDetail.setKimTypeId(kimTypeId);
102 newDetail.setAttributeValue(it.getValue());
103 attrs.add(newDetail);
104 } catch (InstantiationException e) {
105 LOG.error(e.getMessage(), e);
106 } catch (IllegalAccessException e) {
107 LOG.error(e.getMessage(), e);
108 }
109
110 }
111 }
112 return attrs;
113 }
114
115 @Override
116 public String getId() {
117 return id;
118 }
119
120 public void setId(String id) {
121 this.id = id;
122 }
123
124 @Override
125 public String getAttributeValue() {
126 return attributeValue;
127 }
128
129 public void setAttributeValue(String attributeValue) {
130 this.attributeValue = attributeValue;
131 }
132
133 public String getKimAttributeId() {
134 return kimAttributeId;
135 }
136
137 public void setKimAttributeId(String kimAttributeId) {
138 this.kimAttributeId = kimAttributeId;
139 }
140
141 @Override
142 public String getKimTypeId() {
143 return kimTypeId;
144 }
145
146 public void setKimTypeId(String kimTypeId) {
147 this.kimTypeId = kimTypeId;
148 }
149
150 public void setKimType(KimTypeBo kimType) {
151 this.kimType = kimType;
152 }
153
154 public void setKimAttribute(KimAttributeBo kimAttribute) {
155 this.kimAttribute = kimAttribute;
156 }
157 }