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