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 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 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); }