Skip to content

Method: handleGroup(Group)

1: package de.fhdw.wtf.generator.database.tasks;
2:
3: import de.fhdw.wtf.common.ast.Attribute;
4: import de.fhdw.wtf.common.ast.ConstructorOrOperation;
5: import de.fhdw.wtf.common.ast.DatabaseIDSetState;
6: import de.fhdw.wtf.common.ast.Group;
7: import de.fhdw.wtf.common.ast.Model;
8: import de.fhdw.wtf.common.ast.type.AtomicType;
9: import de.fhdw.wtf.common.ast.type.ClassType;
10: import de.fhdw.wtf.common.ast.type.CompositeType;
11: import de.fhdw.wtf.common.ast.type.ListType;
12: import de.fhdw.wtf.common.ast.type.MapType;
13: import de.fhdw.wtf.common.ast.type.ProductType;
14: import de.fhdw.wtf.common.ast.type.SumType;
15: import de.fhdw.wtf.common.ast.type.ThrownType;
16: import de.fhdw.wtf.common.ast.type.TypeProxy;
17: import de.fhdw.wtf.common.ast.visitor.CompositeTypeVisitorException;
18: import de.fhdw.wtf.common.ast.visitor.TypeVisitorException;
19: import de.fhdw.wtf.common.constants.referencer.BaseTypeConstants;
20: import de.fhdw.wtf.common.exception.generation.NoTypeIdSetException;
21: import de.fhdw.wtf.common.exception.walker.TaskException;
22: import de.fhdw.wtf.common.task.TaskExecutor;
23: import de.fhdw.wtf.context.model.collections.PersistentList;
24: import de.fhdw.wtf.context.model.collections.PersistentMap;
25: import de.fhdw.wtf.generator.database.generation.InitialGenerator;
26: import de.fhdw.wtf.persistence.exception.PersistenceException;
27: import de.fhdw.wtf.persistence.facade.TypeManager;
28: import de.fhdw.wtf.persistence.meta.Type;
29: import de.fhdw.wtf.persistence.meta.UnidirectionalAssociation;
30: import de.fhdw.wtf.walker.walker.HelperUtils;
31: import de.fhdw.wtf.walker.walker.SimpleWalkerTask;
32:
33: /**
34: * The {@link AttributeGenerationTask} puts all classes of the Model (AST) into the database. It also adds Type-Ids to
35: * the AST.
36: *
37: */
38: public class AttributeGenerationTask extends SimpleWalkerTask {
39:         
40:         /**
41:          * The name of the default attribute 'This'.
42:          */
43:         private static final String THIS_ATTRIBUTE_NAME = "This";
44:         /**
45:          * The name of the default attribute '$generatedObjects'.
46:          */
47:         private static final String GENERATED_OBJECTS_ATTRIBUTE_NAME = "$generatedObjects";
48:         
49:         /**
50:          * Creates an {@link AttributeGenerationTask}.
51:          *
52:          * @param m
53:          * The associated model.
54:          * @param man
55:          * The task manager to use.
56:          * @param generator
57:          * The generator to use.
58:          */
59:         public AttributeGenerationTask(final Model m, final TaskExecutor man, final InitialGenerator generator) {
60:                 super(m, man);
61:                 this.generator = generator;
62:                 this.persistentList = null;
63:                 this.persistentMap = null;
64:         }
65:         
66:         /**
67:          * The generator to use.
68:          */
69:         private final InitialGenerator generator;
70:         /**
71:          * The type used for persistent lists.
72:          */
73:         private Type persistentList;
74:         /**
75:          * The type used for persistent maps.
76:          */
77:         private Type persistentMap;
78:         
79:         @Override
80:         public void handleAttribute(final Attribute a, final ClassType owner) throws TaskException {
81:                 try {
82:                         a.getAttrType().accept(new AttributeTypeVisitor(a, owner));
83:                 } catch (final TaskException e) {
84:                         throw e;
85:                 } catch (final Exception e) {
86:                         throw new TaskException(e);
87:                 }
88:                 
89:         }
90:         
91:         @Override
92:         public void finalizeTask() {
93:                 // Not involved in this task
94:         }
95:         
96:         @Override
97:         public void handleConstructorOrOperation(final ConstructorOrOperation coo, final ClassType owner)
98:                         throws TaskException {
99:                 // Not involved in this task
100:         }
101:         
102:         @Override
103:         public void handleGroup(final Group g) throws TaskException {
104:                 // Not involved in this task
105:         }
106:         
107:         /**
108:          * Creates the fields 'This' and '$generatedObjects' as attributes of the class for classId in the persistence.
109:          *
110:          * @param c
111:          * The class to create the fields for.
112:          */
113:         @Override
114:         public void handleClass(final ClassType c) throws TaskException {
115:                 long classId;
116:                 try {
117:                         classId = c.getTypeId().getId();
118:                         final TypeManager typeManager = this.generator.getClassFacade().getTypeManager();
119:                         if (!typeManager.existsType("de.fhdw.wtf.context.model.collections.MutableMap")) {
120:                                 this.generator.createClass("de.fhdw.wtf.context.model.collections.MutableMap", false, false);
121:                         }
122:                         this.generator.createUnidirectionalAssociation(THIS_ATTRIBUTE_NAME, false, false, classId, classId);
123:                         this.generator.createUnidirectionalAssociation(GENERATED_OBJECTS_ATTRIBUTE_NAME, false, false, classId, this.generator
124:                                         .mapNameToType(PersistentMap.class.getName()).getId());
125:                 } catch (final NoTypeIdSetException | PersistenceException e) {
126:                         throw new TaskException(e);
127:                 }
128:         }
129:         
130:         @Override
131:         public void beginTask() throws TaskException {
132:                 this.persistentList = this.generator.mapNameToType(PersistentList.class.getName());
133:                 this.persistentMap = this.generator.mapNameToType(PersistentMap.class.getName());
134:         }
135:         
136:         /**
137:          * Handles an attribute's type.
138:          */
139:         private class AttributeTypeVisitor implements TypeVisitorException<Exception> {
140:                 
141:                 /**
142:                  * The attribute to operate on.
143:                  */
144:                 private final Attribute a;
145:                 /**
146:                  * The owner type of the attribute.
147:                  */
148:                 private final ClassType owner;
149:                 
150:                 /**
151:                  * Creates an {@link AttributeTypeVisitor}.
152:                  *
153:                  * @param a
154:                  * The attribute to operate on.
155:                  * @param owner
156:                  * The owner type of the attribute.
157:                  */
158:                 AttributeTypeVisitor(final Attribute a, final ClassType owner) {
159:                         this.a = a;
160:                         this.owner = owner;
161:                 }
162:                 
163:                 @Override
164:                 public void handle(final AtomicType s) throws Exception {
165:                         final UnidirectionalAssociation newAsso =
166:                                         AttributeGenerationTask.this.generator.createUnidirectionalAssociation(
167:                                                         this.a.getName(),
168:                                                         true,
169:                                                         true,
170:                                                         this.owner.getTypeId().getId(),
171:                                                         s.getTypeId().getId());
172:                         this.a.setAttributeId(new DatabaseIDSetState(newAsso.getId()));
173:                 }
174:                 
175:                 @Override
176:                 public void handle(final CompositeType c) throws Exception {
177:                         c.accept(new CompositeAttributeTypeVisitor(this.a, this.owner));
178:                 }
179:                 
180:                 @Override
181:                 public void handle(final TypeProxy s) throws Exception {
182:                         HelperUtils.getReferencedType(s).accept(this);
183:                 }
184:         }
185:         
186:         /**
187:          * Handles an attribute's composite type.
188:          */
189:         private class CompositeAttributeTypeVisitor implements CompositeTypeVisitorException<Exception> {
190:                 /**
191:                  * The attribute to operate on.
192:                  */
193:                 private final Attribute a;
194:                 /**
195:                  * The owner type of the attribute.
196:                  */
197:                 private final ClassType owner;
198:                 
199:                 /**
200:                  * Creates an {@link AttributeTypeVisitor}.
201:                  *
202:                  * @param a
203:                  * The attribute to operate on.
204:                  * @param owner
205:                  * The owner type of the attribute.
206:                  */
207:                 CompositeAttributeTypeVisitor(final Attribute a, final ClassType owner) {
208:                         this.a = a;
209:                         this.owner = owner;
210:                 }
211:                 
212:                 @Override
213:                 public void handle(final ListType list) throws Exception {
214:                         final UnidirectionalAssociation newAsso =
215:                                         AttributeGenerationTask.this.generator.createUnidirectionalAssociation(
216:                                                         CompositeAttributeTypeVisitor.this.a.getName(),
217:                                                         true,
218:                                                         true,
219:                                                         CompositeAttributeTypeVisitor.this.owner.getTypeId().getId(),
220:                                                         AttributeGenerationTask.this.persistentList.getId());
221:                         CompositeAttributeTypeVisitor.this.a.setAttributeId(new DatabaseIDSetState(newAsso.getId()));
222:                 }
223:                 
224:                 @Override
225:                 public void handle(final MapType map) throws Exception {
226:                         final UnidirectionalAssociation newAsso =
227:                                         AttributeGenerationTask.this.generator.createUnidirectionalAssociation(
228:                                                         CompositeAttributeTypeVisitor.this.a.getName(),
229:                                                         true,
230:                                                         true,
231:                                                         CompositeAttributeTypeVisitor.this.owner.getTypeId().getId(),
232:                                                         AttributeGenerationTask.this.persistentMap.getId());
233:                         CompositeAttributeTypeVisitor.this.a.setAttributeId(new DatabaseIDSetState(newAsso.getId()));
234:                 }
235:                 
236:                 @Override
237:                 public void handle(final ProductType product) throws Exception {
238:                         final UnidirectionalAssociation newAsso =
239:                                         AttributeGenerationTask.this.generator.createUnidirectionalAssociation(
240:                                                         CompositeAttributeTypeVisitor.this.a.getName(),
241:                                                         true,
242:                                                         true,
243:                                                         CompositeAttributeTypeVisitor.this.owner.getTypeId().getId(),
244:                                                         product.getTypeId().getId());
245:                         CompositeAttributeTypeVisitor.this.a.setAttributeId(new DatabaseIDSetState(newAsso.getId()));
246:                 }
247:                 
248:                 @Override
249:                 public void handle(final SumType sum) throws Exception {
250:                         final UnidirectionalAssociation newAsso =
251:                                         AttributeGenerationTask.this.generator.createUnidirectionalAssociation(
252:                                                         CompositeAttributeTypeVisitor.this.a.getName(),
253:                                                         true,
254:                                                         true,
255:                                                         CompositeAttributeTypeVisitor.this.owner.getTypeId().getId(),
256:                                                         sum.getTypeId().getId());
257:                         CompositeAttributeTypeVisitor.this.a.setAttributeId(new DatabaseIDSetState(newAsso.getId()));
258:                 }
259:                 
260:                 @Override
261:                 public void handle(final ThrownType thrownType) throws Exception {
262:                         throw new TaskException("A type that should be thrown is not possible in an attribute.");
263:                 }
264:         }
265: }