Skip to content

Package: InitialGenerator$3

InitialGenerator$3

nameinstructionbranchcomplexitylinemethod
handleBaseType(Type)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
handleUserType(UserType)
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
{...}
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

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 abs
61:          * A boolean flag indicating whether this User Type is abstract or not
62:          * @param trans
63:          * A boolean flag indicating whether this User Type is also an Transaction or not.
64:          * @return the new user type
65:          * @throws PersistenceException
66:          * A persistence Exception is thrown if any Error contacting the database occurs.
67:          */
68:         public UserType createClass(final String className, final boolean abs, final boolean trans)
69:                         throws PersistenceException {
70:                 final UserType result = this.classFacade.createUserType(className, abs, false);
71:                 this.types.put(className, result);
72:                 return result;
73:         }
74:         
75:         // /**
76:         // * Deletes the given association and unsets all links of it.
77:         // * @param associationName
78:         // * @throws PersistenceException
79:         // */
80:         // private void deleteAssociation(String associationName) throws
81:         // PersistenceException {
82:         // UnidirectionalAssociation a =
83:         // this.classFacade.getTypeManager().getAssociationForName(associationName);
84:         // // TODO implement!
85:         // }
86:         
87:         /**
88:          * created an unidirectional association between source and target.
89:          *
90:          * @param name
91:          * name of the association
92:          * @param essential
93:          * A boolean flag indicating whether there is an existing object necessary. (when true: 1..)
94:          * @param unique
95:          * A boolean flag indicating whether the association is set-valued. (when true: ..1)
96:          * @param source
97:          * the id of the owning side of the UnidirectionalAssociation
98:          * @param target
99:          * the id of the targeting side of the UnidirectionalAssociation.
100:          * @return Provides a new UnidirectionalAssociation with the given attributes and the next possible ID.
101:          * @throws PersistenceException
102:          * A persistence Exception is thrown if any Error contacting the database occurs.
103:          */
104:         public UnidirectionalAssociation createUnidirectionalAssociation(final String name,
105:                         final boolean essential,
106:                         final boolean unique,
107:                         final long source,
108:                         final long target) throws PersistenceException {
109:                 UserType s;
110:                 Type t;
111:                 final Type s1 = this.classFacade.getTypeManager().getTypeForId(source);
112:                 s = s1.accept(new TypeVisitorReturnException<UserType, TypeOrAssociationNotFoundException>() {
113:                         @Override
114:                         public UserType handleUserType(final UserType t) {
115:                                 return t;
116:                         }
117:                         
118:                         @Override
119:                         public UserType handleBaseType(final Type t) throws TypeOrAssociationNotFoundException {
120:                                 throw new TypeOrAssociationNotFoundException(t.getId());
121:                         }
122:                 });
123:                 t = this.classFacade.getTypeManager().getTypeForId(target);
124:                 return this.classFacade.createUnidirectionalAssociation(name, essential, unique, s, t);
125:         }
126:         
127:         /**
128:          * Creates a map association in the database.
129:          *
130:          * @param name
131:          * The name of the association.
132:          * @param essential
133:          * If true, the association is essential.
134:          * @param source
135:          * The owner type of the association.
136:          * @param target
137:          * The target type of the association.
138:          * @param key
139:          * The key type of the map association.
140:          * @return A {@link MapAssociation} object.
141:          * @throws PersistenceException
142:          * if some database error occurs
143:          */
144:         public MapAssociation createMapAssociation(final String name,
145:                         final boolean essential,
146:                         final long source,
147:                         final long target,
148:                         final long key) throws PersistenceException {
149:                 final UserType s =
150:                                 this.classFacade.getTypeManager().getTypeForId(source)
151:                                                 .accept(new TypeVisitorReturnException<UserType, TypeOrAssociationNotFoundException>() {
152:                                                         @Override
153:                                                         public UserType handleUserType(final UserType type) {
154:                                                                 return type;
155:                                                         }
156:                                                         
157:                                                         @Override
158:                                                         public UserType handleBaseType(final Type type) throws TypeOrAssociationNotFoundException {
159:                                                                 throw new TypeOrAssociationNotFoundException(type.getId());
160:                                                         }
161:                                                 });
162:                 final Type t = this.classFacade.getTypeManager().getTypeForId(target);
163:                 final Type k = this.classFacade.getTypeManager().getTypeForId(key);
164:                 return this.classFacade.createMapAssociation(name, essential, s, t, k);
165:         }
166:         
167:         /**
168:          * Creates a specialization-relationship between two user types.
169:          *
170:          * @param ancestor
171:          * The id of the Ancestor which means the super type in the specialization
172:          * @param descendant
173:          * The id of the descendant which means the sub type in the specialization
174:          * @throws PersistenceException
175:          * A persistence Exception is thrown if any Error contacting the database occurs.
176:          */
177:         public void createSpecialisation(final long ancestor, final long descendant) throws PersistenceException {
178:                 final Type d = this.classFacade.getTypeManager().getTypeForId(descendant);
179:                 final UserType a =
180:                                 this.classFacade.getTypeManager().getTypeForId(ancestor)
181:                                                 .accept(new TypeVisitorReturnException<UserType, TypeOrAssociationNotFoundException>() {
182:                                                         @Override
183:                                                         public UserType handleUserType(final UserType t) throws TypeOrAssociationNotFoundException {
184:                                                                 return t;
185:                                                         }
186:                                                         
187:                                                         @Override
188:                                                         public UserType handleBaseType(final Type t) throws TypeOrAssociationNotFoundException {
189:                                                                 throw new TypeOrAssociationNotFoundException(t.getId());
190:                                                         }
191:                                                 });
192:                 this.classFacade.createSpecializationBetween(a, d);
193:                 
194:         }
195:         
196:         /**
197:          * Returns the underlying class facade.
198:          *
199:          * @return The underlying class facade.
200:          */
201:         public ClassFacade getClassFacade() {
202:                 return this.classFacade;
203:         }
204:         
205: }