public static void buildGetter(OJAnnotatedClass owner, PropertyWrapper propertyWrapper) { OJAnnotatedOperation getter = new OJAnnotatedOperation(propertyWrapper.getter(), propertyWrapper.javaTumlTypePath()); getter.getBody().addToStatements("return this." + propertyWrapper.fieldname()); owner.addToOperations(getter); if (propertyWrapper.isMemberOfAssociationClass()) { getter = new OJAnnotatedOperation( propertyWrapper.associationClassGetter(), propertyWrapper.getAssociationClassJavaTumlTypePath()); getter .getBody() .addToStatements("return this." + propertyWrapper.getAssociationClassFakePropertyName()); owner.addToOperations(getter); // Build getter that returns the AssociationClass for an instance of the other end buildGetAssociationClassForPropertyInstance(owner, propertyWrapper); } // If the property is subsetting another property then add @Override to the getter. // The subsetted property's implementation will add an protected getter with the subsetting // property's name if (!propertyWrapper.getSubsettedProperties().isEmpty()) { // if subsetted property is on an interface then there is no fake implementation, so no // override Property subsettedProperty = propertyWrapper.getSubsettedProperties().get(0); PropertyWrapper subsettedPropertyWrapper = new PropertyWrapper(subsettedProperty); if (!(subsettedProperty.getType() instanceof Interface) && subsettedPropertyWrapper.getOtherEnd().getType() != propertyWrapper.getOtherEnd().getType()) { UmlgGenerationUtil.addOverrideAnnotation(getter); } } }
public static void buildDeleteNotification( OJAnnotatedClass owner, OJAnnotatedOperation delete, PropertyWrapper propertyWrapper) { OJIfStatement ifChanged = new OJIfStatement(); ifChanged.setCondition("true"); PropertyWrapper otherEnd = PropertyWrapper.from(propertyWrapper.getOtherEnd()); if (otherEnd.isMany()) { OJForStatement forStatement = new OJForStatement( otherEnd.getName(), otherEnd.javaBaseTypePath(), "this." + otherEnd.getter() + "()"); forStatement .getBody() .addToStatements(constructNotifierForOtherEndMany(propertyWrapper, otherEnd)); delete.getBody().addToStatements(0, forStatement); } else { ifChanged.addToThenPart(constructNotifier(propertyWrapper)); delete.getBody().addToStatements(0, ifChanged); } owner.addToImports( UmlgClassOperations.getPathName(propertyWrapper.getOwningType()) .append(UmlgClassOperations.propertyEnumName(propertyWrapper.getOwningType()))); owner.addToImports(UmlgGenerationUtil.UmlgNotificationManager); owner.addToImports(UmlgGenerationUtil.ChangeHolder); owner.addToImports(UmlgGenerationUtil.ChangeHolder); owner.addToImports("java.util.Optional"); }
private void addGetObjectRepresentation(PropertyWrapper pWrap, OJAnnotatedClass annotatedClass) { OJAnnotatedOperation get = new OJAnnotatedOperation("get", UmlgRestletGenerationUtil.Representation); UmlgGenerationUtil.addOverrideAnnotation(get); get.addToThrows(UmlgRestletGenerationUtil.ResourceException); UmlgGenerationUtil.addOverrideAnnotation(get); annotatedClass.addToImports(UmlgRestletGenerationUtil.ResourceException); OJTryStatement tryStatement = new OJTryStatement(); OJPathName parentPathName; if (pWrap.getOtherEnd() != null) { parentPathName = UmlgClassOperations.getPathName(pWrap.getOtherEnd().getType()); } else { parentPathName = UmlgClassOperations.getPathName(pWrap.getOwningType()); } tryStatement .getTryPart() .addToStatements( "this." + parentPathName.getLast().toLowerCase() + "Id = " + UmlgRestletGenerationUtil.UmlgURLDecoder.getLast() + ".decode((String)getRequestAttributes().get(\"" + parentPathName.getLast().toLowerCase() + "Id\"))"); annotatedClass.addToImports(UmlgRestletGenerationUtil.UmlgURLDecoder); tryStatement .getTryPart() .addToStatements( parentPathName.getLast() + " parentResource = " + UmlgGenerationUtil.UMLGAccess + "." + UmlgGenerationUtil.getEntity + "(" + parentPathName.getLast().toLowerCase() + "Id" + ")"); annotatedClass.addToImports(parentPathName); buildToJson(pWrap, annotatedClass, tryStatement.getTryPart()); get.getBody().addToStatements(tryStatement); tryStatement.getFinallyPart().addToStatements(UmlgGenerationUtil.UMLGAccess + ".rollback()"); tryStatement.setCatchPart(null); annotatedClass.addToImports(UmlgGenerationUtil.UMLGPathName); annotatedClass.addToImports(UmlgRestletGenerationUtil.JsonRepresentation); annotatedClass.addToOperations(get); }
private void addGetRepresentation( OJAnnotatedInterface enumLookupInf, OJAnnotatedClass enumLookup) { OJAnnotatedOperation getInf = new OJAnnotatedOperation("get", UmlgRestletGenerationUtil.Representation); enumLookupInf.addToOperations(getInf); getInf.addAnnotationIfNew(new OJAnnotationValue(UmlgRestletGenerationUtil.Get, "json")); enumLookupInf.addToOperations(getInf); OJAnnotatedOperation get = new OJAnnotatedOperation("get", UmlgRestletGenerationUtil.Representation); get.addToThrows(UmlgRestletGenerationUtil.ResourceException); enumLookup.addToImports(UmlgRestletGenerationUtil.ResourceException); UmlgGenerationUtil.addOverrideAnnotation(get); enumLookup.addToOperations(get); get.getBody() .addToStatements( "String enumQualifiedName = getQuery().getFirst(\"enumQualifiedName\").getValue();"); get.getBody() .addToStatements( "Class<?> enumClass = " + UmlgGenerationUtil.QualifiedNameClassMap.getLast() + ".INSTANCE.get(enumQualifiedName)"); enumLookup.addToImports(UmlgGenerationUtil.QualifiedNameClassMap); get.getBody() .addToStatements( UmlgGenerationUtil.UmlgEnum.getLast() + "[] enumConstants = (" + UmlgGenerationUtil.UmlgEnum.getLast() + "[])enumClass.getEnumConstants()"); enumLookup.addToImports(UmlgGenerationUtil.UmlgEnum); get.getBody().addToStatements("StringBuilder json = new StringBuilder()"); get.getBody().addToStatements("json.append(\"{\\\"data\\\": [\")"); get.getBody() .addToStatements( "json.append(" + UmlgGenerationUtil.ToJsonUtil.getLast() + ".enumsToJson(Arrays.asList(enumConstants)))"); enumLookup.addToImports("java.util.Arrays"); enumLookup.addToImports(UmlgGenerationUtil.ToJsonUtil); get.getBody().addToStatements("json.append(\"]}\")"); get.getBody() .addToStatements( "return new " + UmlgRestletGenerationUtil.JsonRepresentation.getLast() + "(json.toString())"); enumLookup.addToImports(UmlgRestletGenerationUtil.JsonRepresentation); }
public static void buildGetterForAssociationClass( OJAnnotatedClass ac, PropertyWrapper propertyWrapper) { PropertyWrapper otherEnd = new PropertyWrapper(propertyWrapper.getOtherEnd()); OJAnnotatedOperation getter = new OJAnnotatedOperation(otherEnd.getter(), otherEnd.javaBaseTypePath()); OJAnnotatedField tmpField = new OJAnnotatedField("tmp", otherEnd.javaTumlTypePath(true)); getter.getBody().addToLocals(tmpField); tmpField.setInitExp("this." + otherEnd.fieldname()); OJIfStatement ifFieldNotEmpty = new OJIfStatement("!" + tmpField.getName() + ".isEmpty()"); if (otherEnd.isOrdered()) { ifFieldNotEmpty.addToThenPart("return " + tmpField.getName() + ".get(0)"); } else { ifFieldNotEmpty.addToThenPart("return " + tmpField.getName() + ".iterator().next()"); } ifFieldNotEmpty.addToElsePart("return null"); getter.getBody().addToStatements(ifFieldNotEmpty); ac.addToOperations(getter); }
private static void buildGetAssociationClassForPropertyInstance( OJAnnotatedClass owner, PropertyWrapper propertyWrapper) { OJAnnotatedOperation getAC = new OJAnnotatedOperation( propertyWrapper.associationClassGetterForProperty(), propertyWrapper.getAssociationClassPathName()); getAC.addParam(propertyWrapper.fieldname(), propertyWrapper.javaBaseTypePath()); OJForStatement forAC = new OJForStatement( "ac", propertyWrapper.getAssociationClassPathName(), "this." + propertyWrapper.getAssociationClassFakePropertyName()); OJIfStatement ifStatement = new OJIfStatement( "ac." + propertyWrapper.getter() + "().equals(" + propertyWrapper.fieldname() + ")", "return ac"); forAC.getBody().addToStatements(ifStatement); getAC.getBody().addToStatements(forAC); getAC.getBody().addToStatements("return null"); owner.addToOperations(getAC); }
public static OJAnnotatedOperation buildSetter(OJAnnotatedClass owner, PropertyWrapper pWrap) { OJAnnotatedOperation setter = new OJAnnotatedOperation(pWrap.setter()); if (pWrap.isReadOnly()) { setter.setVisibility(OJVisibilityKind.PROTECTED); } if (!pWrap.isMemberOfAssociationClass()) { setter.addParam(pWrap.fieldname(), pWrap.javaTypePath()); } else { setter.addParam( pWrap.getAssociationClassFakePropertyName(), pWrap.javaTypePathWithAssociationClass()); } setter.getBody().addToStatements(pWrap.clearer() + "()"); OJIfStatement ifNotNull; if (!pWrap.isMemberOfAssociationClass()) { ifNotNull = new OJIfStatement(pWrap.fieldname() + " != null"); ifNotNull.addToThenPart(pWrap.adder() + "(" + pWrap.fieldname() + ")"); } else { ifNotNull = new OJIfStatement(pWrap.getAssociationClassFakePropertyName() + " != null"); ifNotNull.addToThenPart( pWrap.adder() + "(" + pWrap.getAssociationClassFakePropertyName() + ")"); } setter.getBody().addToStatements(ifNotNull); owner.addToOperations(setter); return setter; }
private void addPostResource( PropertyWrapper pWrap, OJAnnotatedClass annotatedClass, OJPathName parentPathName) { OJAnnotatedOperation add = new OJAnnotatedOperation("add"); add.setVisibility(OJVisibilityKind.PRIVATE); add.addToParameters( new OJParameter( "resultMap", new OJPathName("java.util.Map") .addToGenerics( new OJPathName("Class") .addToGenerics("? extends " + pWrap.javaBaseTypePath().getLast())) .addToGenerics( "List<" + UmlgRestletGenerationUtil.UmlgNodeJsonHolder.getLast() + ">"))); add.addToParameters(new OJParameter("parentResource", parentPathName)); add.addToParameters( new OJParameter( "propertyMap", new OJPathName("java.util.Map").addToGenerics("String").addToGenerics("Object"))); annotatedClass.addToOperations(add); OJField qualifiedName = new OJField("qualifiedName", "String"); qualifiedName.setInitExp("(String)propertyMap.get(\"qualifiedName\")"); add.getBody().addToLocals(qualifiedName); OJField baseTumlClass = new OJField( "baseTumlClass", new OJPathName("Class").addToGenerics(pWrap.javaBaseTypePath())); baseTumlClass.setInitExp( UmlgGenerationUtil.UmlgSchemaFactory.getLast() + ".getUmlgSchemaMap().get(qualifiedName)"); annotatedClass.addToImports(UmlgGenerationUtil.UmlgSchemaFactory); add.getBody().addToLocals(baseTumlClass); OJField sb = new OJField( "objectList", "List<" + UmlgRestletGenerationUtil.UmlgNodeJsonHolder.getLast() + ">"); add.getBody().addToLocals(sb); OJIfStatement ifSbExist = new OJIfStatement("!resultMap.containsKey(baseTumlClass)"); ifSbExist.addToThenPart( "objectList = new ArrayList<" + UmlgRestletGenerationUtil.UmlgNodeJsonHolder.getLast() + ">()"); ifSbExist.addToThenPart("resultMap.put(baseTumlClass, objectList)"); ifSbExist.addToElsePart("objectList = resultMap.get(baseTumlClass)"); add.getBody().addToStatements(ifSbExist); OJTryStatement tryInstantiate = new OJTryStatement(); add.getBody().addToStatements(tryInstantiate); if (pWrap.isComposite()) { PropertyWrapper otherEndPWrap = new PropertyWrapper(pWrap.getOtherEnd()); if (!pWrap.isMemberOfAssociationClass()) { OJField constructor = new OJField( "constructor", new OJPathName("java.lang.reflect.Constructor") .addToGenerics(pWrap.javaBaseTypePath())); constructor.setInitExp( "baseTumlClass.getConstructor(" + otherEndPWrap.javaBaseTypePath().getLast() + ".class)"); tryInstantiate.getTryPart().addToLocals(constructor); tryInstantiate .getTryPart() .addToStatements( pWrap.javaBaseTypePath().getLast() + " childResource = constructor.newInstance(parentResource)"); tryInstantiate.getTryPart().addToStatements("childResource.fromJson(propertyMap)"); tryInstantiate .getTryPart() .addToStatements( "objectList.add(new " + UmlgRestletGenerationUtil.UmlgNodeJsonHolder.getLast() + "(childResource))"); } else { PropertyWrapper otherEnd = new PropertyWrapper(pWrap.getOtherEnd()); OJField constructor = new OJField( "constructor", new OJPathName("java.lang.reflect.Constructor") .addToGenerics(pWrap.javaBaseTypePath())); constructor.setInitExp( "baseTumlClass.getConstructor(" + otherEndPWrap.javaBaseTypePath().getLast() + ".class, " + otherEnd.getAssociationClassPathName().getLast() + ".class)"); tryInstantiate.getTryPart().addToLocals(constructor); // TODO check this out, it creates a duplicate as the fromJson creates the association class tryInstantiate .getTryPart() .addToStatements( otherEnd.getAssociationClassPathName().getLast() + " " + otherEnd.getAssociationClassFakePropertyName() + " = new " + otherEnd.getAssociationClassPathName().getLast() + "(true)"); tryInstantiate .getTryPart() .addToStatements( otherEnd.getAssociationClassFakePropertyName() + ".fromJson((Map<String, Object>) propertyMap.get(\"" + otherEnd.getAssociationClassFakePropertyName() + "\"))"); tryInstantiate .getTryPart() .addToStatements( pWrap.javaBaseTypePath().getLast() + " childResource = constructor.newInstance(parentResource, " + otherEnd.getAssociationClassFakePropertyName() + ")"); tryInstantiate.getTryPart().addToStatements("childResource.fromJson(propertyMap)"); tryInstantiate .getTryPart() .addToStatements( "objectList.add(new " + UmlgRestletGenerationUtil.UmlgNodeJsonHolder.getLast() + "(childResource, \"" + otherEnd.getAssociationClassFakePropertyName() + "\", " + otherEnd.getAssociationClassFakePropertyName() + "))"); annotatedClass.addToImports(otherEnd.getAssociationClassPathName()); } } else { tryInstantiate.getTryPart().addToStatements("Object id = propertyMap.get(\"id\")"); // Need to remove the one from the map, otherwise the from will also add the one and create // another association class PropertyWrapper otherEndPWrap = new PropertyWrapper(pWrap.getOtherEnd()); tryInstantiate .getTryPart() .addToStatements("propertyMap.remove(\"" + otherEndPWrap.fieldname() + "\")"); tryInstantiate .getTryPart() .addToStatements( pWrap.javaBaseTypePath().getLast() + " childResource = " + UmlgGenerationUtil.UMLGAccess + "." + UmlgGenerationUtil.getEntity + "(id)"); if (!pWrap.isMemberOfAssociationClass()) { // // tryInstantiate.getTryPart().addToStatements("childResource.fromJson(propertyMap)"); tryInstantiate .getTryPart() .addToStatements("parentResource." + pWrap.adder() + "(childResource)"); tryInstantiate .getTryPart() .addToStatements( "objectList.add(new " + UmlgRestletGenerationUtil.UmlgNodeJsonHolder.getLast() + "(childResource))"); } else { // // tryInstantiate.getTryPart().addToStatements("childResource.fromJson(propertyMap)"); PropertyWrapper otherEnd = new PropertyWrapper(pWrap.getOtherEnd()); // TODO check this out, it creates a duplicate as the fromJson creates the association class tryInstantiate .getTryPart() .addToStatements( otherEnd.getAssociationClassPathName().getLast() + " " + otherEnd.getAssociationClassFakePropertyName() + " = new " + otherEnd.getAssociationClassPathName().getLast() + "(true)"); tryInstantiate .getTryPart() .addToStatements( otherEnd.getAssociationClassFakePropertyName() + ".fromJson((Map<String, Object>) propertyMap.get(\"" + otherEnd.getAssociationClassFakePropertyName() + "\"))"); tryInstantiate .getTryPart() .addToStatements( "parentResource." + pWrap.adder() + "(childResource, " + otherEnd.getAssociationClassFakePropertyName() + ")"); tryInstantiate .getTryPart() .addToStatements( "objectList.add(new " + UmlgRestletGenerationUtil.UmlgNodeJsonHolder.getLast() + "(childResource, \"" + otherEnd.getAssociationClassFakePropertyName() + "\", " + otherEnd.getAssociationClassFakePropertyName() + "))"); annotatedClass.addToImports(otherEnd.getAssociationClassPathName()); } } annotatedClass.addToImports(pWrap.javaBaseTypePath()); if (pWrap.isOrdered()) { // TODO } else { // TODO } tryInstantiate.setCatchParam(new OJParameter("e", "Exception")); tryInstantiate.getCatchPart().addToStatements("throw new RuntimeException(e)"); }
private void addPutResource( PropertyWrapper pWrap, OJAnnotatedClass annotatedClass, OJPathName parentPathName) { OJAnnotatedOperation put = new OJAnnotatedOperation("put"); put.setVisibility(OJVisibilityKind.PRIVATE); put.addToParameters( new OJParameter( "resultMap", new OJPathName("java.util.Map") .addToGenerics( new OJPathName("Class") .addToGenerics("? extends " + pWrap.javaBaseTypePath().getLast())) .addToGenerics( "List<" + UmlgRestletGenerationUtil.UmlgNodeJsonHolder.getLast() + ">"))); if (pWrap.isOrdered()) { put.addToParameters(new OJParameter("parentResource", parentPathName)); } put.addToParameters( new OJParameter( "propertyMap", new OJPathName("java.util.Map").addToGenerics("String").addToGenerics("Object"))); annotatedClass.addToOperations(put); OJBlock firstBlock = new OJBlock(); firstBlock.addToStatements("Object id = propertyMap.get(\"id\")"); firstBlock.addToStatements( pWrap.javaBaseTypePath().getLast() + " childResource = " + UmlgGenerationUtil.UMLGAccess + "." + UmlgGenerationUtil.getEntity + "(id)"); annotatedClass.addToImports(pWrap.javaBaseTypePath()); firstBlock.addToStatements("childResource.fromJson(propertyMap)"); if (pWrap.isOrdered()) { // Place the child at the correct index firstBlock.addToStatements( "Integer index = (Integer)propertyMap.get(\"" + UmlgRestletGenerationUtil._INDEX + "\")"); OJIfStatement ifIndexNotNull = new OJIfStatement("index != null"); if (!pWrap.isMemberOfAssociationClass()) { ifIndexNotNull.addToThenPart("parentResource." + pWrap.remover() + "(childResource)"); ifIndexNotNull.addToThenPart("parentResource." + pWrap.adder() + "(index, childResource)"); } else { ifIndexNotNull.addToThenPart( "parentResource." + pWrap.associationClassMoverForProperty() + "(index, childResource)"); } firstBlock.addToStatements(ifIndexNotNull); } put.getBody().addToStatements(firstBlock); OJBlock secondBlock = new OJBlock(); OJField baseTumlClass = new OJField( "baseTumlClass", new OJPathName("Class") .addToGenerics("? extends " + pWrap.javaBaseTypePath().getLast())); baseTumlClass.setInitExp("childResource.getClass()"); secondBlock.addToLocals(baseTumlClass); OJField sb = new OJField( "objectList", "List<" + UmlgRestletGenerationUtil.UmlgNodeJsonHolder.getLast() + ">"); secondBlock.addToLocals(sb); OJIfStatement ifSbExist = new OJIfStatement("!resultMap.containsKey(baseTumlClass)"); ifSbExist.addToThenPart( "objectList = new ArrayList<" + UmlgRestletGenerationUtil.UmlgNodeJsonHolder.getLast() + ">()"); ifSbExist.addToThenPart("resultMap.put(baseTumlClass, objectList)"); ifSbExist.addToElsePart("objectList = resultMap.get(baseTumlClass)"); secondBlock.addToStatements(ifSbExist); secondBlock.addToStatements( "objectList.add(new " + UmlgRestletGenerationUtil.UmlgNodeJsonHolder.getLast() + "(childResource))"); put.getBody().addToStatements(secondBlock); }
private void addPostObjectRepresentation(PropertyWrapper pWrap, OJAnnotatedClass annotatedClass) { OJAnnotatedOperation post = new OJAnnotatedOperation("post", UmlgRestletGenerationUtil.Representation); post.addToParameters(new OJParameter("entity", UmlgRestletGenerationUtil.Representation)); post.addToThrows(UmlgRestletGenerationUtil.ResourceException); annotatedClass.addToImports(UmlgRestletGenerationUtil.ResourceException); UmlgGenerationUtil.addOverrideAnnotation(post); UmlgGenerationUtil.addSuppressWarning(post); PropertyWrapper otherEndPWrap = new PropertyWrapper(pWrap.getOtherEnd()); OJPathName parentPathName = otherEndPWrap.javaBaseTypePath(); post.getBody() .addToStatements( "this." + parentPathName.getLast().toLowerCase() + "Id = " + UmlgRestletGenerationUtil.UmlgURLDecoder.getLast() + ".decode((String)getRequestAttributes().get(\"" + parentPathName.getLast().toLowerCase() + "Id\"))"); annotatedClass.addToImports(UmlgRestletGenerationUtil.UmlgURLDecoder); post.getBody() .addToStatements( parentPathName.getLast() + " parentResource = " + UmlgGenerationUtil.UMLGAccess + "." + UmlgGenerationUtil.getEntity + "(" + parentPathName.getLast().toLowerCase() + "Id" + ")"); OJTryStatement ojTryStatement = new OJTryStatement(); OJField mapper = new OJField("mapper", UmlgGenerationUtil.ObjectMapper); mapper.setInitExp( UmlgGenerationUtil.ObjectMapperFactory.getLast() + ".INSTANCE.getObjectMapper()"); annotatedClass.addToImports(UmlgGenerationUtil.ObjectMapperFactory); ojTryStatement.getTryPart().addToLocals(mapper); OJPathName pathName = new OJPathName("java.util.Map").addToGenerics("String").addToGenerics("Object"); OJAnnotatedField entityText = new OJAnnotatedField("entityText", "String"); entityText.setInitExp("entity.getText()"); ojTryStatement.getTryPart().addToLocals(entityText); OJField resultMap = new OJField( "resultMap", new OJPathName("java.util.Map") .addToGenerics( new OJPathName("Class<? extends " + pWrap.javaBaseTypePath().getLast() + ">")) .addToGenerics( "List<" + UmlgRestletGenerationUtil.UmlgNodeJsonHolder.getLast() + ">")); resultMap.setInitExp( "new HashMap<Class<? extends " + pWrap.javaBaseTypePath().getLast() + ">, List<" + UmlgRestletGenerationUtil.UmlgNodeJsonHolder.getLast() + ">>()"); annotatedClass.addToImports("java.util.HashMap"); annotatedClass.addToImports("java.util.List"); annotatedClass.addToImports(UmlgRestletGenerationUtil.UmlgNodeJsonHolder); ojTryStatement.getTryPart().addToLocals(resultMap); ojTryStatement .getTryPart() .addToStatements( pathName.getLast() + " overloaded = mapper.readValue(" + entityText.getName() + ", Map.class)"); ojTryStatement.getTryPart().addToStatements("Object o = overloaded.get(\"insert\")"); // Insert OJIfStatement ifInsert = new OJIfStatement("o != null"); OJIfStatement ifArrayForInsert = new OJIfStatement("o instanceof ArrayList"); OJPathName genericsForArray = new OJPathName("java.util.Map").addToGenerics("String").addToGenerics("Object"); OJField array = new OJField("array", new OJPathName("java.util.List").addToGenerics(genericsForArray)); array.setInitExp("(ArrayList<Map<String, Object>>)o"); ifArrayForInsert.getThenPart().addToLocals(array); ifInsert.addToThenPart(ifArrayForInsert); ojTryStatement.getTryPart().addToStatements(ifInsert); OJForStatement forArray = new OJForStatement( "overloadedJsonMap", new OJPathName("java.util.Map") .addToGenerics(new OJPathName("String")) .addToGenerics(new OJPathName("Object")), "array"); ifArrayForInsert.addToThenPart(forArray); forArray.getBody().addToStatements("add(resultMap, parentResource, overloadedJsonMap)"); OJField map = new OJField( "overloadedJsonMap", new OJPathName("java.util.Map").addToGenerics("String").addToGenerics("Object")); map.setInitExp("(Map<String, Object>) o"); ifArrayForInsert.setElsePart(new OJBlock()); ifArrayForInsert.getElsePart().addToLocals(map); ifArrayForInsert .getElsePart() .addToStatements("add(resultMap, parentResource, overloadedJsonMap)"); addPostResource(pWrap, annotatedClass, parentPathName); // Delete ojTryStatement.getTryPart().addToStatements("o = overloaded.get(\"delete\")"); OJIfStatement ifDelete = new OJIfStatement("o != null"); OJIfStatement ifArrayForDelete = new OJIfStatement("o instanceof ArrayList"); genericsForArray = new OJPathName("java.util.Map").addToGenerics("String").addToGenerics("Object"); array = new OJField("array", new OJPathName("java.util.ArrayList").addToGenerics(genericsForArray)); array.setInitExp("(ArrayList<Map<String, Object>>)o"); ifArrayForDelete.getThenPart().addToLocals(array); ifDelete.addToThenPart(ifArrayForDelete); ojTryStatement.getTryPart().addToStatements(ifDelete); forArray = new OJForStatement( "map", new OJPathName("java.util.Map") .addToGenerics(new OJPathName("String")) .addToGenerics(new OJPathName("Object")), "array"); ifArrayForDelete.addToThenPart(forArray); if (pWrap.isComposite()) { forArray.getBody().addToStatements("delete(map)"); } else { forArray.getBody().addToStatements("delete(parentResource, map)"); } map = new OJField( "map", new OJPathName("java.util.Map").addToGenerics("String").addToGenerics("Object")); map.setInitExp("(Map<String, Object>) o"); ifArrayForDelete.setElsePart(new OJBlock()); ifArrayForDelete.getElsePart().addToLocals(map); if (pWrap.isComposite()) { ifArrayForDelete.getElsePart().addToStatements("delete(map)"); } else { ifArrayForDelete.getElsePart().addToStatements("delete(parentResource, map)"); } addDeleteResource(pWrap, annotatedClass, parentPathName); // Update ojTryStatement.getTryPart().addToStatements("o = overloaded.get(\"update\")"); OJIfStatement ifUpdate = new OJIfStatement("o != null"); OJIfStatement ifArrayForUpdate = new OJIfStatement("o instanceof ArrayList"); genericsForArray = new OJPathName("java.util.Map").addToGenerics("String").addToGenerics("Object"); array = new OJField("array", new OJPathName("java.util.ArrayList").addToGenerics(genericsForArray)); array.setInitExp("(ArrayList<Map<String, Object>>)o"); ifArrayForUpdate.getThenPart().addToLocals(array); ifUpdate.addToThenPart(ifArrayForUpdate); ojTryStatement.getTryPart().addToStatements(ifUpdate); forArray = new OJForStatement( "overloadedJsonMap", new OJPathName("java.util.Map") .addToGenerics(new OJPathName("String")) .addToGenerics(new OJPathName("Object")), "array"); ifArrayForUpdate.addToThenPart(forArray); if (pWrap.isOrdered()) { forArray.getBody().addToStatements("put(resultMap, parentResource, overloadedJsonMap)"); } else { forArray.getBody().addToStatements("put(resultMap, overloadedJsonMap)"); } map = new OJField( "overloadedJsonMap", new OJPathName("java.util.Map").addToGenerics("String").addToGenerics("Object")); map.setInitExp("(Map<String, Object>) o"); ifArrayForUpdate.setElsePart(new OJBlock()); ifArrayForUpdate.getElsePart().addToLocals(map); if (pWrap.isOrdered()) { // Include the parent as it will be needed to add the child at a particular index ifArrayForUpdate .getElsePart() .addToStatements("put(resultMap, parentResource, overloadedJsonMap)"); } else { ifArrayForUpdate.getElsePart().addToStatements("put(resultMap, overloadedJsonMap)"); } addPutResource(pWrap, annotatedClass, parentPathName); // Check if transaction needs commiting commitOrRollback(ojTryStatement); OJBlock jsonResultBlock = new OJBlock(); ojTryStatement.getTryPart().addToStatements(jsonResultBlock); OJField result = new OJField("result", "java.lang.StringBuilder"); result.setInitExp("new StringBuilder(\"[\")"); jsonResultBlock.addToLocals(result); OJField count = new OJField("count", "int"); count.setInitExp("1"); jsonResultBlock.addToLocals(count); OJForStatement forConcreteClassifiers = new OJForStatement( "baseClass", new OJPathName("Class") .addToGenerics("? extends " + pWrap.javaBaseTypePath().getLast()), "resultMap.keySet()"); jsonResultBlock.addToStatements(forConcreteClassifiers); if (pWrap.isOne()) { forConcreteClassifiers.getBody().addToStatements("result.append(\"{\\\"data\\\": \")"); } else { forConcreteClassifiers.getBody().addToStatements("result.append(\"{\\\"data\\\": [\")"); } OJField objectList = new OJField( "objectList", new OJPathName("java.util.List") .addToGenerics(UmlgRestletGenerationUtil.UmlgNodeJsonHolder)); objectList.setInitExp("resultMap.get(baseClass)"); forConcreteClassifiers.getBody().addToLocals(objectList); OJField objectListCount = new OJField("objectListCount", new OJPathName("int")); objectListCount.setInitExp("1"); forConcreteClassifiers.getBody().addToLocals(objectListCount); OJForStatement forObjectList = new OJForStatement("object", UmlgRestletGenerationUtil.UmlgNodeJsonHolder, "objectList"); forObjectList.getBody().addToStatements("result.append(object.toJson())"); OJIfStatement ifObjectListCountSmallerThanSize = new OJIfStatement("objectListCount++ < objectList.size()", "result.append(\",\")"); forObjectList.getBody().addToStatements(ifObjectListCountSmallerThanSize); forConcreteClassifiers.getBody().addToStatements(forObjectList); if (pWrap.isOne()) { forConcreteClassifiers.getBody().addToStatements("result.append(\",\")"); } else { forConcreteClassifiers.getBody().addToStatements("result.append(\"],\")"); } forConcreteClassifiers.getBody().addToStatements("result.append(\" \\\"meta\\\" : {\")"); forConcreteClassifiers .getBody() .addToStatements( "result.append(\"\\\"qualifiedName\\\": \\\"" + pWrap.getQualifiedName() + "\\\"\")"); forConcreteClassifiers.getBody().addToStatements("result.append(\", \\\"to\\\": \")"); OJIfStatement ifClassInstanceOf = null; Set<Classifier> concreteImplementations = UmlgClassOperations.getConcreteImplementations((Classifier) pWrap.getType()); if (!concreteImplementations.isEmpty()) { ifClassInstanceOf = new OJIfStatement(); forConcreteClassifiers.getBody().addToStatements(ifClassInstanceOf); } boolean first = true; for (Classifier concreteImplementation : concreteImplementations) { OJBlock ojIfBlock; if (first) { first = false; ifClassInstanceOf.setCondition( "baseClass.equals(" + UmlgClassOperations.getPathName(concreteImplementation).getLast() + ".class)"); ojIfBlock = ifClassInstanceOf.getThenPart(); } else { ojIfBlock = ifClassInstanceOf.addToElseIfCondition( "baseClass.equals(" + UmlgClassOperations.getPathName(concreteImplementation).getLast() + ".class)", ""); } ojIfBlock.addToStatements( "result.append(" + UmlgClassOperations.propertyEnumName(concreteImplementation) + ".asJson())"); annotatedClass.addToImports( UmlgClassOperations.getPathName(concreteImplementation) .append(UmlgClassOperations.propertyEnumName(concreteImplementation))); ojIfBlock.addToStatements("result.append(\", \\\"from\\\": \")"); Classifier owningType = (Classifier) pWrap.getOwningType(); ojIfBlock.addToStatements( "result.append(" + UmlgClassOperations.propertyEnumName(owningType) + ".asJson())"); annotatedClass.addToImports( UmlgClassOperations.getPathName(owningType) .append(UmlgClassOperations.propertyEnumName(owningType))); ojIfBlock.addToStatements("result.append(\"}\")"); } OJIfStatement ifLast = new OJIfStatement("count++ == resultMap.size()"); ifLast.addToThenPart("result.append(\"}\")"); ifLast.addToElsePart("result.append(\"},\")"); forConcreteClassifiers.getBody().addToStatements(ifLast); if (!concreteImplementations.isEmpty()) { ifClassInstanceOf.addToElsePart( "throw new IllegalStateException(\"Unknown type \" + baseClass.getName())"); } jsonResultBlock.addToStatements("result.append(\"]\")"); jsonResultBlock.addToStatements( "return new " + UmlgRestletGenerationUtil.JsonRepresentation.getLast() + "(result.toString())"); ojTryStatement.setCatchParam(new OJParameter("e", new OJPathName("java.lang.Exception"))); ojTryStatement.getCatchPart().addToStatements(UmlgGenerationUtil.UMLGAccess + ".rollback()"); ojTryStatement .getCatchPart() .addToStatements( "throw " + UmlgRestletGenerationUtil.UmlgExceptionUtilFactory.getLast() + ".getTumlExceptionUtil().handle(e)"); annotatedClass.addToImports(UmlgRestletGenerationUtil.UmlgExceptionUtilFactory); post.getBody().addToStatements(ojTryStatement); annotatedClass.addToImports(parentPathName); annotatedClass.addToImports(UmlgGenerationUtil.UMLGPathName); annotatedClass.addToImports(UmlgRestletGenerationUtil.JsonRepresentation); annotatedClass.addToOperations(post); }
private void addDeleteResource( PropertyWrapper pWrap, OJAnnotatedClass annotatedClass, OJPathName parentPathName) { OJAnnotatedOperation delete = new OJAnnotatedOperation("delete"); delete.setVisibility(OJVisibilityKind.PRIVATE); annotatedClass.addToOperations(delete); annotatedClass.addToImports(pWrap.javaBaseTypePath()); if (pWrap.isComposite()) { delete.addToParameters( new OJParameter( "propertyMap", new OJPathName("java.util.Map").addToGenerics("String").addToGenerics("Object"))); delete.getBody().addToStatements("Object id = propertyMap.get(\"id\")"); delete .getBody() .addToStatements( pWrap.javaBaseTypePath().getLast() + " childResource = " + UmlgGenerationUtil.UMLGAccess + "." + UmlgGenerationUtil.getEntity + "(id)"); delete.getBody().addToStatements("childResource.delete()"); } else { delete.addToParameters(new OJParameter("parentResource", parentPathName)); delete.addToParameters( new OJParameter( "propertyMap", new OJPathName("java.util.Map").addToGenerics("String").addToGenerics("Object"))); delete.getBody().addToStatements("Object id = propertyMap.get(\"id\")"); delete .getBody() .addToStatements( pWrap.javaBaseTypePath().getLast() + " childResource = " + UmlgGenerationUtil.UMLGAccess + "." + UmlgGenerationUtil.getEntity + "(id)"); delete.getBody().addToStatements("parentResource." + pWrap.remover() + "(childResource)"); } }
private void addServerResourceToRouterEnum( PropertyWrapper pWrap, OJAnnotatedClass annotatedClass) { OJEnum routerEnum = (OJEnum) this.workspace.findOJClass(UmlgRestletGenerationUtil.RestletRouterEnum.toJavaString()); OJEnumLiteral ojLiteral = new OJEnumLiteral( UmlgClassOperations.getPathName(pWrap.getOwningType()).getLast() + "_" + pWrap.fieldname()); OJField uri = new OJField(); uri.setType(new OJPathName("String")); uri.setInitExp( "\"/" + UmlgClassOperations.getPathName(pWrap.getOwningType()).getLast().toLowerCase() + "s/{" + UmlgClassOperations.getPathName(pWrap.getOwningType()).getLast().toLowerCase() + "Id}/" + pWrap.fieldname() + "\""); ojLiteral.addToAttributeValues(uri); OJField serverResourceClassField = new OJField(); serverResourceClassField.setType(new OJPathName("java.lang.Class")); serverResourceClassField.setInitExp(annotatedClass.getName() + ".class"); ojLiteral.addToAttributeValues(serverResourceClassField); routerEnum.addToImports(annotatedClass.getPathName()); routerEnum.addToImports(UmlgRestletGenerationUtil.ServerResource); routerEnum.addToLiterals(ojLiteral); OJAnnotatedOperation attachAll = routerEnum.findOperation("attachAll", UmlgRestletGenerationUtil.Router); attachAll .getBody() .addToStatements(routerEnum.getName() + "." + ojLiteral.getName() + ".attach(router)"); // //Add the url for post/put to the resource // ojLiteral = new // OJEnumLiteral(UmlgClassOperations.getPathName(pWrap.getOwningType()).getLast() + "_" + // pWrap.fieldname()); // // uri = new OJField(); // uri.setType(new OJPathName("String")); // uri.setInitExp("\"" + // UmlgClassOperations.getPathName(pWrap.getOwningType()).getLast().toLowerCase() + "s/{" // + // UmlgClassOperations.getPathName(pWrap.getOwningType()).getLast().toLowerCase() + "Id}/" + // pWrap.fieldname() + "\""); // ojLiteral.addToAttributeValues(uri); // // serverResourceClassField = new OJField(); // serverResourceClassField.setType(new OJPathName("java.lang.Class")); // serverResourceClassField.setInitExp(annotatedClass.getName() + ".class"); // ojLiteral.addToAttributeValues(serverResourceClassField); // routerEnum.addToImports(annotatedClass.getPathName()); // routerEnum.addToImports(UmlgRestletGenerationUtil.ServerResource); // // routerEnum.addToLiterals(ojLiteral); // // attachAll.getBody().addToStatements(routerEnum.getName() + "." + ojLiteral.getName() + // ".attach(router)"); }
public static void buildManyAdder( OJAnnotatedClass owner, PropertyWrapper propertyWrapper, boolean indexed, boolean ignoreInverse) { OJAnnotatedOperation adder = new OJAnnotatedOperation(propertyWrapper.adder()); if (!propertyWrapper.isMemberOfAssociationClass()) { if (indexed) { adder.addParam("index", "int"); } adder.addParam(propertyWrapper.fieldname(), propertyWrapper.javaTypePath()); } else { if (indexed) { adder.addParam("index", "int"); } adder.addParam( propertyWrapper.getAssociationClassFakePropertyName(), propertyWrapper.javaTypePathWithAssociationClass()); } if (!(owner instanceof OJAnnotatedInterface)) { if (!propertyWrapper.hasQualifiers()) { if (!propertyWrapper.isMemberOfAssociationClass()) { OJIfStatement ifNotNull = new OJIfStatement("!" + propertyWrapper.fieldname() + ".isEmpty()"); ifNotNull.addToThenPart( "this." + propertyWrapper.fieldname() + ".addAll(" + propertyWrapper.fieldname() + ")"); adder.getBody().addToStatements(ifNotNull); } else { // iterate the association class set as it contains a Pair OJForStatement associationClassPairs = new OJForStatement( "pair", propertyWrapper.getAssociationClassPair(), propertyWrapper.getAssociationClassFakePropertyName()); associationClassPairs .getBody() .addToStatements(propertyWrapper.adder() + "(pair.getFirst(), pair.getSecond())"); adder.getBody().addToStatements(associationClassPairs); } } else { String elementName = "_" + propertyWrapper.fieldname().substring(0, 1); OJForStatement forAll = new OJForStatement( elementName, propertyWrapper.javaBaseTypePath(), propertyWrapper.fieldname()); forAll .getBody() .addToStatements("this." + propertyWrapper.adder() + "(" + elementName + ")"); adder.getBody().addToStatements(forAll); } } owner.addToOperations(adder); OJAnnotatedOperation singleAdder = new OJAnnotatedOperation( !ignoreInverse ? propertyWrapper.adder() : propertyWrapper.adderIgnoreInverse()); if (indexed) { singleAdder.addParam("index", "int"); } singleAdder.addParam(propertyWrapper.fieldname(), propertyWrapper.javaBaseTypePath()); if (propertyWrapper.isMemberOfAssociationClass()) { singleAdder.addParam( StringUtils.uncapitalize(propertyWrapper.getAssociationClass().getName()), UmlgClassOperations.getPathName(propertyWrapper.getAssociationClass())); } if (!(owner instanceof OJAnnotatedInterface)) { PropertyWrapper otherEnd = new PropertyWrapper(propertyWrapper.getOtherEnd()); if ( /*For bags the one side can have many edges to the same element*/ propertyWrapper.isUnique() && propertyWrapper.hasOtherEnd() && !propertyWrapper.isEnumeration() && otherEnd.isOne()) { OJIfStatement ifNotNull2 = new OJIfStatement(propertyWrapper.fieldname() + " != null"); ifNotNull2.addToThenPart(propertyWrapper.fieldname() + "." + otherEnd.clearer() + "()"); ifNotNull2.addToThenPart( propertyWrapper.fieldname() + ".initialiseProperty(" + UmlgClassOperations.propertyEnumName(otherEnd.getOwningType()) + "." + otherEnd.fieldname() + ", false)"); ifNotNull2.addToThenPart( propertyWrapper.remover() + "(" + propertyWrapper.fieldname() + ")"); owner.addToImports( UmlgClassOperations.getPathName(otherEnd.getOwningType()) .append(UmlgClassOperations.propertyEnumName(otherEnd.getOwningType()))); singleAdder.getBody().addToStatements(ifNotNull2); } OJIfStatement ifNotNull = new OJIfStatement(propertyWrapper.fieldname() + " != null"); // Add in validations OJBlock block; if (propertyWrapper.isDataType()) { OJField failedConstraints = new OJField( "violations", new OJPathName("java.util.List") .addToGenerics(UmlgGenerationUtil.UmlgConstraintViolation)); failedConstraints.setInitExp( propertyWrapper.validator() + "(" + propertyWrapper.fieldname() + ")"); ifNotNull.getThenPart().addToLocals(failedConstraints); OJIfStatement ifValidated = new OJIfStatement("violations.isEmpty()"); ifValidated.addToElsePart( "throw new " + UmlgGenerationUtil.UmlgConstraintViolationException.getLast() + "(violations)"); owner.addToImports(UmlgGenerationUtil.UmlgConstraintViolationException); ifNotNull.addToThenPart(ifValidated); block = ifValidated.getThenPart(); } else { block = ifNotNull.getThenPart(); } if (!propertyWrapper.isMemberOfAssociationClass()) { if (!indexed) { if (!ignoreInverse) { block.addToStatements( "this." + propertyWrapper.fieldname() + ".add(" + propertyWrapper.fieldname() + ")"); } else { block.addToStatements( "this." + propertyWrapper.fieldname() + ".addIgnoreInverse(" + propertyWrapper.fieldname() + ")"); } } else { block.addToStatements( "this." + propertyWrapper.fieldname() + ".add(index, " + propertyWrapper.fieldname() + ")"); } } else { if (!indexed) { block.addToStatements( "this." + propertyWrapper.fieldname() + ".add(" + propertyWrapper.fieldname() + ", " + StringUtils.uncapitalize(propertyWrapper.getAssociationClass().getName()) + ")"); } else { block.addToStatements( "this." + propertyWrapper.fieldname() + ".add(index, " + propertyWrapper.fieldname() + ", " + StringUtils.uncapitalize(propertyWrapper.getAssociationClass().getName()) + ")"); } } singleAdder.getBody().addToStatements(ifNotNull); } owner.addToOperations(singleAdder); // //Add change listener // if (propertyWrapper.isChangedListener()) { // PropertyChangeNotificationBuilder.buildChangeNotification(owner, singleAdder, // propertyWrapper, PropertyChangeNotificationBuilder.CHANGE_TYPE.ADD); // } }