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