public String resolveEquals(DataObject dataObject, String indent) { StringBuilder head = new StringBuilder(); // head.append(EOL); head.append(indent + "@Override" + EOL); head.append(indent + "public boolean equals(Object o) {" + EOL); head.append(indent + TAB + "if (this == o) return true;" + EOL); head.append(indent + TAB + "if (o == null || getClass() != o.getClass()) return false;" + EOL); head.append( indent + TAB + dataObject.getClassName() + " that = (" + dataObject.getClassName() + ")o;" + EOL); StringBuilder end = new StringBuilder(); end.append(indent + "}"); StringBuilder sb = new StringBuilder(); Map<String, ObjectProperty> props = dataObject.getProperties(); boolean hasTerms = false; if (props != null && props.size() > 0) { for (String propName : props.keySet()) { ObjectProperty prop = props.get(propName); String _propName = toJavaVar(propName); if (prop.getAnnotation(org.kie.api.definition.type.Key.class.getName()) != null) { // Construction: "if (<_propName> != null ? !<_propName>.equals(that.<_propName>) : // that.<_propName> != null) return false;" sb.append(indent + TAB + "if ("); sb.append(_propName) .append(" != null ? !") .append(_propName) .append(".equals(that.") .append(_propName) .append(")"); sb.append(" : that.").append(_propName).append(" != null").append(") return false;"); sb.append(EOL); hasTerms = true; } } } if (hasTerms) { sb.append(indent + TAB + "return true;" + EOL); head.append(sb); head.append(end); return head.toString(); } else { return ""; } }
public String resolveHashCode(DataObject dataObject, String indent) { StringBuilder head = new StringBuilder(); // head.append(EOL); head.append(indent + "@Override" + EOL); head.append(indent + "public int hashCode() {" + EOL); head.append(indent + TAB + "int result = 17;" + EOL); StringBuilder end = new StringBuilder(); end.append(indent + "}"); StringBuilder sb = new StringBuilder(); Map<String, ObjectProperty> props = dataObject.getProperties(); boolean hasTerms = false; if (props != null && props.size() > 0) { for (String propName : props.keySet()) { ObjectProperty prop = props.get(propName); String _propName = toJavaVar(propName); if (prop.getAnnotation(org.kie.api.definition.type.Key.class.getName()) != null) { // Construction: "result = 13 * result + (<_propName> != null ? <_propName>.hashCode() : // 0);" sb.append(indent + TAB + "result = 13 * result + (") .append(_propName) .append(" != null ? ") .append(_propName) .append(".hashCode() : 0);"); sb.append(EOL); hasTerms = true; } } } if (hasTerms) { sb.append(indent + TAB + "return result;" + EOL); head.append(sb); head.append(end); return head.toString(); } else { return ""; } }
public String resolveConstructor( DataObject dataObject, List<ObjectProperty> properties, String indent) { StringBuilder head = new StringBuilder(); StringBuilder body = new StringBuilder(); head.append(indent + "public " + dataObject.getName() + "("); if (properties != null && properties.size() > 0) { boolean isFirst = true; String propertyName; for (ObjectProperty property : properties) { if (!isFirst) { head.append(", "); body.append(EOL); } propertyName = toJavaVar(property.getName()); head.append(resolveAttributeType(property)); head.append(" "); head.append(propertyName); body.append(indent); body.append(indent); body.append("this."); body.append(propertyName); body.append(" = "); body.append(propertyName); body.append(";"); isFirst = false; } body.append(EOL); } head.append(") {" + EOL); head.append(body); head.append(indent + "}"); return head.toString(); }