Skip to content

Method: InitialGenerator()

1: package de.fhdw.wtf.generator.database.generation;
2:
3: import java.util.HashMap;
4: import java.util.Map;
5:
6: import de.fhdw.wtf.persistence.exception.PersistenceException;
7: import de.fhdw.wtf.persistence.exception.TypeOrAssociationNotFoundException;
8: import de.fhdw.wtf.persistence.facade.ClassFacade;
9: import de.fhdw.wtf.persistence.facade.OracleClassFacadeImplementation;
10: import de.fhdw.wtf.persistence.facade.OracleDatabaseManager;
11: import de.fhdw.wtf.persistence.meta.MapAssociation;
12: import de.fhdw.wtf.persistence.meta.Type;
13: import de.fhdw.wtf.persistence.meta.TypeVisitorReturnException;
14: import de.fhdw.wtf.persistence.meta.UnidirectionalAssociation;
15: import de.fhdw.wtf.persistence.meta.UserType;
16:
17: public class InitialGenerator {
18:         
19:         /**
20:          * The underlying class facade for database access.
21:          */
22:         private final ClassFacade classFacade;
23:         /**
24:          * Maps type names to database Types.
25:          */
26:         private final Map<String, Type> types;
27:         
28:         /**
29:          * Creates an InitialGenerator.
30:          *
31:          * @throws PersistenceException
32:          * if some database error occurs.
33:          */
34:         public InitialGenerator() throws PersistenceException {
35:                 this.classFacade = new OracleClassFacadeImplementation(OracleDatabaseManager.getInstance());
36:                 this.classFacade.initialize();
37:                 this.types = new HashMap<>();
38:                 // this.objectFacade = new
39:                 // OracleObjectFacadeImplementation(OracleDatabaseManager.getInstance(),
40:                 // this.classFacade.getTypeManager());
41:                 
42:         }
43:         
44:         /**
45:          * Maps a type name to a database Type object.
46:          *
47:          * @param typeName
48:          * The name of the type.
49:          * @return The database Type object or null if no such object exists.
50:          */
51:         public Type mapNameToType(final String typeName) {
52:                 return this.types.get(typeName);
53:         }
54:         
55:         /**
56:          * creates a new user type.
57:          *
58:          * @param className
59:          * The Name of the User Type.
60:          * @param trans
61:          * @return the new user type
62:          * @throws PersistenceException
63:          * A persistence Exception is thrown if any Error contacting the database occurs.
64:          */
65:         public UserType createClass(final String className, final boolean abs, final boolean trans)
66:                         throws PersistenceException {
67:                 final UserType result = this.classFacade.createUserType(className, abs, trans);
68:                 this.types.put(className, result);
69:                 return result;
70:         }
71:         
72:         // /**
73:         // * Deletes the given association and unsets all links of it.
74:         // * @param associationName
75:         // * @throws PersistenceException
76:         // */
77:         // private void deleteAssociation(String associationName) throws
78:         // PersistenceException {
79:         // UnidirectionalAssociation a =
80:         // this.classFacade.getTypeManager().getAssociationForName(associationName);
81:         // // TODO implement!
82:         // }
83:         
84:         /**
85:          * created an unidirectional association between source and target.
86:          *
87:          * @param name
88:          * name of the association
89:          * @param essential
90:          * A boolean flag indicating whether there is an existing object necessary. (when true: 1..)
91:          * @param unique
92:          * A boolean flag indicating whether the association is set-valued. (when true: ..1)
93:          * @param source
94:          * the id of the owning side of the UnidirectionalAssociation
95:          * @param target
96:          * the id of the targeting side of the UnidirectionalAssociation.
97:          * @return Provides a new UnidirectionalAssociation with the given attributes and the next possible ID.
98:          * @throws PersistenceException
99:          * A persistence Exception is thrown if any Error contacting the database occurs.
100:          */
101:         public UnidirectionalAssociation createUnidirectionalAssociation(final String name,
102:                         final boolean essential,
103:                         final boolean unique,
104:                         final long source,
105:                         final long target) throws PersistenceException {
106:                 UserType s;
107:                 Type t;
108:                 final Type s1 = this.classFacade.getTypeManager().getTypeForId(source);
109:                 s = s1.accept(new TypeVisitorReturnException<UserType, TypeOrAssociationNotFoundException>() {
110:                         @Override
111:                         public UserType handleUserType(final UserType t) {
112:                                 return t;
113:                         }
114:                         
115:                         @Override
116:                         public UserType handleBaseType(final Type t) throws TypeOrAssociationNotFoundException {
117:                                 throw new TypeOrAssociationNotFoundException(t.getId());
118:                         }
119:                 });
120:                 t = this.classFacade.getTypeManager().getTypeForId(target);
121:                 return this.classFacade.createUnidirectionalAssociation(name, essential, unique, s, t);
122:         }
123:         
124:         /**
125:          * Creates a map association in the database.
126:          *
127:          * @param name
128:          * The name of the association.
129:          * @param essential
130:          * If true, the association is essential.
131:          * @param source
132:          * The owner type of the association.
133:          * @param target
134:          * The target type of the association.
135:          * @param key
136:          * The key type of the map association.
137:          * @return A {@link MapAssociation} object.
138:          * @throws PersistenceException
139:          * if some database error occurs
140:          */
141:         public MapAssociation createMapAssociation(final String name,
142:                         final boolean essential,
143:                         final long source,
144:                         final long target,
145:                         final long key) throws PersistenceException {
146:                 final UserType s =
147:                                 this.classFacade.getTypeManager().getTypeForId(source)
148:                                                 .accept(new TypeVisitorReturnException<UserType, TypeOrAssociationNotFoundException>() {
149:                                                         @Override
150:                                                         public UserType handleUserType(final UserType type) {
151:                                                                 return type;
152:                                                         }
153:                                                         
154:                                                         @Override
155:                                                         public UserType handleBaseType(final Type type) throws TypeOrAssociationNotFoundException {
156:                                                                 throw new TypeOrAssociationNotFoundException(type.getId());
157:                                                         }
158:                                                 });
159:                 final Type t = this.classFacade.getTypeManager().getTypeForId(target);
160:                 final Type k = this.classFacade.getTypeManager().getTypeForId(key);
161:                 return this.classFacade.createMapAssociation(name, essential, s, t, k);
162:         }
163:         
164:         /**
165:          * Creates a specialization-relationship between two user types.
166:          *
167:          * @param ancestor
168:          * The id of the Ancestor which means the super type in the specialization
169:          * @param descendant
170:          * The id of the descendant which means the sub type in the specialization
171:          * @throws PersistenceException
172:          * A persistence Exception is thrown if any Error contacting the database occurs.
173:          */
174:         public void createSpecialisation(final long ancestor, final long descendant) throws PersistenceException {
175:                 final Type d = this.classFacade.getTypeManager().getTypeForId(descendant);
176:                 final UserType a =
177:                                 this.classFacade.getTypeManager().getTypeForId(ancestor)
178:                                                 .accept(new TypeVisitorReturnException<UserType, TypeOrAssociationNotFoundException>() {
179:                                                         @Override
180:                                                         public UserType handleUserType(final UserType t) throws TypeOrAssociationNotFoundException {
181:                                                                 return t;
182:                                                         }
183:                                                         
184:                                                         @Override
185:                                                         public UserType handleBaseType(final Type t) throws TypeOrAssociationNotFoundException {
186:                                                                 throw new TypeOrAssociationNotFoundException(t.getId());
187:                                                         }
188:                                                 });
189:                 this.classFacade.createSpecializationBetween(a, d);
190:                 
191:         }
192:         
193:         /**
194:          * Returns the underlying class facade.
195:          *
196:          * @return The underlying class facade.
197:          */
198:         public ClassFacade getClassFacade() {
199:                 return this.classFacade;
200:         }
201:         
202: }