1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.impl.common.attribute;
17
18 import javax.persistence.Transient;
19
20 import org.apache.commons.collections.CollectionUtils;
21 import org.apache.commons.lang.StringUtils;
22 import org.kuali.rice.kim.api.common.attribute.KimAttributeDataContract;
23 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
24 import org.kuali.rice.kim.api.type.KimType;
25 import org.kuali.rice.kim.api.type.KimTypeAttribute;
26 import org.kuali.rice.kim.api.type.KimTypeInfoService;
27 import org.kuali.rice.kim.impl.type.KimTypeBo;
28 import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
29 import org.kuali.rice.krad.util.ObjectUtils;
30
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37 public abstract class KimAttributeDataBo extends PersistableBusinessObjectBase implements KimAttributeDataContract {
38 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KimAttributeDataBo.class);
39 private static final long serialVersionUID = 1L;
40
41 private static KimTypeInfoService kimTypeInfoService;
42
43 private String id;
44 private String attributeValue;
45 private String kimAttributeId;
46 private KimAttributeBo kimAttribute;
47 private String kimTypeId;
48 @Transient
49 private KimTypeBo kimType;
50
51 public abstract void setAssignedToId(String s);
52
53 @Override
54 public KimAttributeBo getKimAttribute() {
55 if(ObjectUtils.isNull(this.kimAttribute)
56 && StringUtils.isNotBlank(kimAttributeId)) {
57 this.refreshReferenceObject("kimAttribute");
58 }
59 return kimAttribute;
60 }
61
62 @Override
63 public KimTypeBo getKimType() {
64 if (kimType == null && StringUtils.isNotEmpty(id)) {
65 kimType = KimTypeBo.from(KimApiServiceLocator.getKimTypeInfoService().getKimType(kimTypeId));
66 }
67 return kimType;
68 }
69
70 public static <T extends KimAttributeDataBo> Map<String, String> toAttributes(Collection<T> bos) {
71 Map<String, String> m = new HashMap<String, String>();
72 if(CollectionUtils.isNotEmpty(bos)) {
73 for (T it : bos) {
74 if (it != null) {
75 KimTypeAttribute attribute = null;
76 if ( it.getKimType() != null ) {
77 attribute = KimTypeBo.to(it.getKimType()).getAttributeDefinitionById(it.getKimAttributeId());
78 }
79 if ( attribute != null ) {
80 m.put(attribute.getKimAttribute().getAttributeName(), it.getAttributeValue());
81 } else {
82 m.put(it.getKimAttribute().getAttributeName(), it.getAttributeValue());
83 }
84 }
85 }
86 }
87 return m;
88 }
89
90
91 public static <T extends KimAttributeDataBo> List<T> createFrom(Class<T> type, Map<String, String> attributes, String kimTypeId) {
92 if (attributes == null) {
93
94 return new ArrayList<T>();
95 }
96 List<T> attrs = new ArrayList<T>();
97 for (Map.Entry<String, String> it : attributes.entrySet()) {
98
99 KimTypeAttribute attr = getKimTypeInfoService().getKimType(kimTypeId).getAttributeDefinitionByName(it.getKey());
100 KimType theType = getKimTypeInfoService().getKimType(kimTypeId);
101 if (attr != null && StringUtils.isNotBlank(it.getValue())) {
102 try {
103 T newDetail = type.newInstance();
104 newDetail.setKimAttributeId(attr.getKimAttribute().getId());
105 newDetail.setKimAttribute(KimAttributeBo.from(attr.getKimAttribute()));
106 newDetail.setKimTypeId(kimTypeId);
107 newDetail.setKimType(KimTypeBo.from(theType));
108 newDetail.setAttributeValue(it.getValue());
109 attrs.add(newDetail);
110 } catch (InstantiationException e) {
111 LOG.error(e.getMessage(), e);
112 } catch (IllegalAccessException e) {
113 LOG.error(e.getMessage(), e);
114 }
115
116 }
117 }
118 return attrs;
119 }
120
121 @Override
122 public String getId() {
123 return id;
124 }
125
126 public void setId(String id) {
127 this.id = id;
128 }
129
130 @Override
131 public String getAttributeValue() {
132 return attributeValue;
133 }
134
135 public void setAttributeValue(String attributeValue) {
136 this.attributeValue = attributeValue;
137 }
138
139 public String getKimAttributeId() {
140 return kimAttributeId;
141 }
142
143 public void setKimAttributeId(String kimAttributeId) {
144 this.kimAttributeId = kimAttributeId;
145 }
146
147 @Override
148 public String getKimTypeId() {
149 return kimTypeId;
150 }
151
152 public void setKimTypeId(String kimTypeId) {
153 this.kimTypeId = kimTypeId;
154 }
155
156 public void setKimType(KimTypeBo kimType) {
157 this.kimType = kimType;
158 }
159
160 public void setKimAttribute(KimAttributeBo kimAttribute) {
161 this.kimAttribute = kimAttribute;
162 }
163
164
165 public static KimTypeInfoService getKimTypeInfoService() {
166 if (kimTypeInfoService==null) {
167 kimTypeInfoService = KimApiServiceLocator.getKimTypeInfoService();
168 }
169 return kimTypeInfoService;
170 }
171
172 public static void setKimTypeInfoService(KimTypeInfoService kimTypeInfoService) {
173 KimAttributeDataBo.kimTypeInfoService = kimTypeInfoService;
174 }
175
176 }