|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2020 [Open Lowcode SAS](https://openlowcode.com/) |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0 . |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + ********************************************************************************/ |
| 10 | + |
| 11 | +package org.openlowcode.design.data.properties.basic; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.util.ArrayList; |
| 15 | + |
| 16 | +import org.openlowcode.design.data.ChoiceCategory; |
| 17 | +import org.openlowcode.design.data.ChoiceValue; |
| 18 | +import org.openlowcode.design.data.DataObjectDefinition; |
| 19 | +import org.openlowcode.design.data.Property; |
| 20 | +import org.openlowcode.design.data.PropertyGenerics; |
| 21 | +import org.openlowcode.design.data.SimpleChoiceCategory; |
| 22 | +import org.openlowcode.design.generation.SourceGenerator; |
| 23 | +import org.openlowcode.design.generation.StringFormatter; |
| 24 | +import org.openlowcode.design.module.Module; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author <a href="https://openlowcode.com/" rel="nofollow">Open Lowcode |
| 28 | + * SAS</a> |
| 29 | + * |
| 30 | + * @param <E> |
| 31 | + */ |
| 32 | +public class SubtypeCompanion<E extends DataObjectDefinition> |
| 33 | + extends |
| 34 | + Property<SubtypeCompanion<E>> { |
| 35 | + |
| 36 | + Subtype<?> subtypeformainobject; |
| 37 | + DataObjectDefinition mainobjet; |
| 38 | + private DataObjectDefinition mainobject; |
| 39 | + private ChoiceValue[] typesforcompanion; |
| 40 | + private SimpleChoiceCategory subtypecategory; |
| 41 | + private ChoiceCategory types; |
| 42 | + private StoredObject storedobject; |
| 43 | + |
| 44 | + public SubtypeCompanion(DataObjectDefinition mainobject, ChoiceValue[] typesforcompanion) { |
| 45 | + super("SUBTYPECOMPANION"); |
| 46 | + if (mainobject == null) |
| 47 | + throw new RuntimeException("Main object is not defined"); |
| 48 | + subtypeformainobject = (Subtype<?>) mainobject.getPropertyByName("SUBTYPE"); |
| 49 | + this.mainobject = mainobject; |
| 50 | + this.typesforcompanion = typesforcompanion; |
| 51 | + if (typesforcompanion == null) |
| 52 | + throw new RuntimeException("SubtypeCompanion property for object " + mainobject.getName() |
| 53 | + + " does not have a type for companion choice (null array)"); |
| 54 | + if (typesforcompanion.length == 0) |
| 55 | + throw new RuntimeException("SubtypeCompanion property for object " + mainobject.getName() |
| 56 | + + " does not have a type for companion choice (zero element array)"); |
| 57 | + types = typesforcompanion[0].getParent(); |
| 58 | + this.addChoiceCategoryHelper("LISTOFSUBTYPES", types); |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void controlAfterParentDefinition() { |
| 64 | + this.storedobject = (StoredObject) parent.getPropertyByName("STOREDOBJECT"); |
| 65 | + if (this.storedobject == null) |
| 66 | + throw new RuntimeException("Subtypecompanion is dependent on storedobject"); |
| 67 | + this.addDependentProperty(storedobject); |
| 68 | + this.addPropertyGenerics(new PropertyGenerics("MAINOBJECT", mainobject, new UniqueIdentified())); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public String[] getPropertyInitMethod() { |
| 73 | + // TODO Auto-generated method stub |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public String[] getPropertyExtractMethod() { |
| 79 | + // TODO Auto-generated method stub |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public String[] getPropertyDeepCopyStatement() { |
| 85 | + // TODO Auto-generated method stub |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public ArrayList<DataObjectDefinition> getExternalObjectDependence() { |
| 91 | + // TODO Auto-generated method stub |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void setFinalSettings() { |
| 97 | + if (this.getParent().getPropertyByName("UNIQUEIDENTIFIED") != null) |
| 98 | + throw new RuntimeException("Object " + this.getParent().getName() |
| 99 | + + " has both subtype companion and unique identified, this is not possible"); |
| 100 | + subtypeformainobject = (Subtype<?>) mainobject.getPropertyByName("SUBTYPE"); |
| 101 | + if (subtypeformainobject == null) |
| 102 | + throw new RuntimeException( |
| 103 | + "Main object " + this.mainobject.getName() + " does not have the property SUBTYPE, so object " |
| 104 | + + this.getParent().getName() + " cannot reference it as a main object for subtype"); |
| 105 | + subtypecategory = subtypeformainobject.getSubTypes(); |
| 106 | + for (int i = 0; i < typesforcompanion.length; i++) { |
| 107 | + ChoiceValue thissubtype = typesforcompanion[i]; |
| 108 | + |
| 109 | + if (!subtypecategory.isKeyPresent(thissubtype.getName())) |
| 110 | + throw new RuntimeException("Subtype " + thissubtype + " for companion object " |
| 111 | + + this.getParent().getName() + " is not part of the category for main object " |
| 112 | + + subtypeformainobject.getParent().getName()); |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public String getJavaType() { |
| 119 | + // TODO Auto-generated method stub |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void writeDependentClass(SourceGenerator sg, Module module) throws IOException { |
| 125 | + |
| 126 | + sg.wl("import " + types.getParentModule().getPath() + ".data.choice." |
| 127 | + + StringFormatter.formatForJavaClass(types.getName()) + "ChoiceDefinition;"); |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments