1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.datadictionary;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
20 import org.kuali.rice.krad.datadictionary.parse.BeanTag;
21 import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
22 import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 @BeanTag(name = "primitiveAttributeDefinition-bean")
40 public class PrimitiveAttributeDefinition extends DataDictionaryDefinitionBase {
41 private static final long serialVersionUID = -715128943756700821L;
42
43 protected String sourceName;
44 protected String targetName;
45
46 public PrimitiveAttributeDefinition() {}
47
48
49
50
51 @BeanTagAttribute(name = "sourceName")
52 public String getSourceName() {
53 return sourceName;
54 }
55
56
57
58
59
60
61 public void setSourceName(String sourceName) {
62 if (StringUtils.isBlank(sourceName)) {
63 throw new IllegalArgumentException("invalid (blank) sourceName");
64 }
65
66 this.sourceName = sourceName;
67 }
68
69
70
71
72 @BeanTagAttribute(name = "targetName")
73 public String getTargetName() {
74 return targetName;
75 }
76
77
78
79
80
81
82 public void setTargetName(String targetName) {
83 if (StringUtils.isBlank(targetName)) {
84 throw new IllegalArgumentException("invalid (blank) targetName");
85 }
86
87 this.targetName = targetName;
88 }
89
90
91
92
93
94
95
96 public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass) {
97 if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, sourceName)) {
98 throw new AttributeValidationException("unable to find attribute '"
99 + sourceName
100 + "' in relationship class '"
101 + rootBusinessObjectClass
102 + "' ("
103 + ""
104 + ")");
105 }
106 if (!DataDictionary.isPropertyOf(otherBusinessObjectClass, targetName)) {
107 throw new AttributeValidationException(
108 "unable to find attribute '" + targetName + "' in related class '" + otherBusinessObjectClass
109 .getName() + "' (" + "" + ")");
110 }
111
112 Class sourceClass = DataDictionary.getAttributeClass(rootBusinessObjectClass, sourceName);
113 Class targetClass = DataDictionary.getAttributeClass(otherBusinessObjectClass, targetName);
114 if ((null == sourceClass && null != targetClass) || (null != sourceClass && null == targetClass) || !StringUtils
115 .equals(sourceClass.getName(), targetClass.getName())) {
116 String sourceClassName = rootBusinessObjectClass.getName();
117 String targetClassName = otherBusinessObjectClass.getName();
118 String sourcePath = sourceClassName + "." + sourceName;
119 String targetPath = targetClassName + "." + targetName;
120
121
122 if ((sourcePath != null && !StringUtils.contains(sourcePath, ".principalId")) && (targetPath != null
123 && !StringUtils.contains(targetPath, ".principalId"))) {
124 throw new AttributeValidationException("source attribute '"
125 + sourcePath
126 + "' ("
127 + sourceClass
128 + ") and target attribute '"
129 + targetPath
130 + "' ("
131 + targetClass
132 + ") are of differing types ("
133 + ""
134 + ")");
135 }
136 }
137 }
138
139
140
141
142
143
144 public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass,
145 ValidationTrace tracer) {
146 tracer.addBean(this.getClass().getSimpleName(), ValidationTrace.NO_BEAN_ID);
147
148 try {
149 if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, sourceName)) {
150 String currentValues[] = {"attribute = " + getSourceName(), "class = " + rootBusinessObjectClass};
151 tracer.createError("Unable to find attribute on class", currentValues);
152 }
153 } catch (RuntimeException ex) {
154 String currentValues[] = {"attribute = " + getSourceName(), "class = " + rootBusinessObjectClass,
155 "Exception = " + ex.getMessage()};
156 tracer.createError("Unable to find attribute on class", currentValues);
157 }
158 try {
159 if (!DataDictionary.isPropertyOf(otherBusinessObjectClass, targetName)) {
160 String currentValues[] = {"attribute = " + getTargetName(), "class = " + otherBusinessObjectClass};
161 tracer.createError("Unable to find attribute on class", currentValues);
162 }
163 } catch (RuntimeException ex) {
164 String currentValues[] = {"attribute = " + getTargetName(), "class = " + otherBusinessObjectClass,
165 "Exception = " + ex.getMessage()};
166 tracer.createError("Unable to find attribute on class", currentValues);
167 }
168 try {
169 Class sourceClass = DataDictionary.getAttributeClass(rootBusinessObjectClass, sourceName);
170 Class targetClass = DataDictionary.getAttributeClass(otherBusinessObjectClass, targetName);
171 if ((null == sourceClass && null != targetClass)
172 || (null != sourceClass && null == targetClass)
173 || !StringUtils.equals(sourceClass.getName(), targetClass.getName())) {
174 String sourceClassName = rootBusinessObjectClass.getName();
175 String targetClassName = otherBusinessObjectClass.getName();
176 String sourcePath = sourceClassName + "." + sourceName;
177 String targetPath = targetClassName + "." + targetName;
178
179
180 if ((sourcePath != null && !StringUtils.contains(sourcePath, ".principalId")) && (targetPath != null
181 && !StringUtils.contains(targetPath, ".principalId"))) {
182 String currentValues[] = {"source = " + sourcePath + "' (" + sourceClass + ")",
183 "target = " + targetPath + "' (" + targetClass + ")"};
184 tracer.createError("Source and target of different types", currentValues);
185 }
186 }
187 } catch (RuntimeException ex) {
188 String currentValues[] = {"Exception = " + ex.getMessage()};
189 tracer.createError("Unable to validate property", currentValues);
190 }
191 }
192
193
194
195
196 @Override
197 public String toString() {
198 return "PrimitiveAttributeDefinition (" + getSourceName() + "," + getTargetName() + ")";
199 }
200 }