001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.tutorial.persistance;
034:
035: import com.flexive.shared.CacheAdmin;
036: import com.flexive.shared.EJBLookup;
037: import com.flexive.shared.FxLanguage;
038: import com.flexive.shared.exceptions.FxApplicationException;
039: import com.flexive.shared.interfaces.AssignmentEngine;
040: import com.flexive.shared.interfaces.ContentEngine;
041: import com.flexive.shared.interfaces.TypeEngine;
042: import com.flexive.shared.security.ACL;
043: import com.flexive.shared.structure.*;
044: import com.flexive.shared.value.FxString;
045:
046: /**
047: * Tutorial on how to work with FxTypes
048: *
049: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
050: */
051: public class StructureTutorialExample {
052:
053: public final static String BASE_TYPE = "Customer";
054:
055: /**
056: * Create a new type for customer data
057: *
058: * @throws FxApplicationException on errors
059: */
060: public void createType() throws FxApplicationException {
061: TypeEngine typeEngine = EJBLookup.getTypeEngine();
062:
063: FxString typeDesc = new FxString(true, FxLanguage.ENGLISH,
064: "A generic customer");
065: typeDesc.setTranslation(FxLanguage.GERMAN,
066: "Ein generischer Kunde");
067: ACL customerACL = CacheAdmin.getEnvironment().getACL(
068: ACL.Category.STRUCTURE.getDefaultId());
069:
070: FxTypeEdit type = FxTypeEdit.createNew(BASE_TYPE, typeDesc,
071: customerACL);
072:
073: //Type retrieval variant 1:
074: typeEngine.save(type);
075: FxType typeByName = CacheAdmin.getEnvironment().getType(
076: BASE_TYPE);
077:
078: //Type retrieval variant 2:
079: // long typeId = typeEngine.save(type);
080: // FxType typeById = CacheAdmin.getEnvironment().getType(typeId);
081:
082: //Invalid Type retrieval - this will not work!
083: // FxType typeByIdInvalid = CacheAdmin.getEnvironment().
084: // getType(typeEngine.save(type));
085: }
086:
087: /**
088: * Add a street to the customer type
089: *
090: * @param typeId id of the type
091: * @throws FxApplicationException on errors
092: */
093: public void addNameProperty(long typeId)
094: throws FxApplicationException {
095: AssignmentEngine assignmentEngine = EJBLookup
096: .getAssignmentEngine();
097: ACL customerACL = CacheAdmin.getEnvironment().getACL(
098: ACL.Category.STRUCTURE.getDefaultId());
099:
100: FxPropertyEdit name = FxPropertyEdit.createNew("Name",
101: new FxString("Name of the person"), new FxString(
102: "Enter the persons name"),
103: FxMultiplicity.MULT_1_1, customerACL,
104: FxDataType.String1024);
105: assignmentEngine.createProperty(typeId, name
106: .setAutoUniquePropertyName(true), "/");
107: }
108:
109: /**
110: * Reuse the Caption property from the ROOT type
111: *
112: * @param typeId the type to assign the reused caption property
113: * @throws FxApplicationException on errors
114: */
115: public void reuseCaptionProperty(long typeId)
116: throws FxApplicationException {
117: AssignmentEngine assignmentEngine = EJBLookup
118: .getAssignmentEngine();
119:
120: //'regular' way to reuse a property assignment
121: assignmentEngine.save(FxPropertyAssignmentEdit.createNew(
122: (FxPropertyAssignment) CacheAdmin.getEnvironment()
123: .getAssignment("ROOT/CAPTION"),
124: CacheAdmin.getEnvironment().getType(BASE_TYPE),
125: "CAPTION", "/").setEnabled(true), false);
126:
127: //same as above with a different alias using the convenience "reuse" method
128: assignmentEngine.save(FxPropertyAssignmentEdit.reuse(
129: "ROOT/CAPTION", BASE_TYPE, "/", "AnotherCaption"),
130: false);
131: }
132:
133: /**
134: * Create a simple group for address data
135: *
136: * @param typeId id of the type
137: * @throws FxApplicationException on errors
138: */
139: public void createAddressGroup(long typeId)
140: throws FxApplicationException {
141: AssignmentEngine assignmentEngine = EJBLookup
142: .getAssignmentEngine();
143: ACL customerACL = CacheAdmin.getEnvironment().getACL(
144: ACL.Category.STRUCTURE.getDefaultId());
145:
146: assignmentEngine.createGroup(typeId, FxGroupEdit.createNew(
147: "Address", new FxString("The customers address"),
148: new FxString("Enter the customers address here"), true,
149: FxMultiplicity.MULT_1_1).setAssignmentGroupMode(
150: GroupMode.AnyOf), "/");
151:
152: FxPropertyEdit street = FxPropertyEdit.createNew("Street",
153: new FxString("Streetname"),
154: new FxString("Enter the street name"),
155: FxMultiplicity.MULT_1_1, customerACL,
156: FxDataType.String1024).setAutoUniquePropertyName(true);
157: FxPropertyEdit zip = FxPropertyEdit.createNew("ZIP",
158: new FxString("ZIP Code"),
159: new FxString("Enter the ZIP code"),
160: FxMultiplicity.MULT_1_1, customerACL,
161: FxDataType.String1024).setAutoUniquePropertyName(true);
162: assignmentEngine.createProperty(typeId, street, "/Address");
163: assignmentEngine.createProperty(typeId, zip, "/Address");
164: }
165:
166: /**
167: * Reuse the address group as "DeliverAddress" and rename the original
168: * address group to "BillingAddress"
169: *
170: * @throws FxApplicationException on errors
171: */
172: public void reuseGroup() throws FxApplicationException {
173: AssignmentEngine assignmentEngine = EJBLookup
174: .getAssignmentEngine();
175: FxType customer = CacheAdmin.getEnvironment()
176: .getType(BASE_TYPE);
177:
178: FxGroupAssignment addressGroup = (FxGroupAssignment) CacheAdmin
179: .getEnvironment().getAssignment(
180: customer.getName() + "/Address");
181:
182: assignmentEngine.save(FxGroupAssignmentEdit.createNew(
183: addressGroup, customer, "DeliveryAddress", "/")
184: .setMultiplicity(FxMultiplicity.MULT_0_1), true);
185: assignmentEngine.save(addressGroup.asEditable().setAlias(
186: "BillingAddress"), false);
187: }
188:
189: /**
190: * Create a "special customer" that gets a discount by inheriting
191: * from the existing customer and adding a discount property.
192: *
193: * @throws FxApplicationException on errors
194: */
195: public void deriveType() throws FxApplicationException {
196: TypeEngine typeEngine = EJBLookup.getTypeEngine();
197: AssignmentEngine assignmentEngine = EJBLookup
198: .getAssignmentEngine();
199:
200: //fetch base type (customer)
201: FxType customer = CacheAdmin.getEnvironment()
202: .getType(BASE_TYPE);
203:
204: //create derived type
205: FxTypeEdit special = FxTypeEdit.createNew("SpecialCustomer",
206: new FxString("A very special customer"), customer
207: .getACL(), customer);
208: long specialId = typeEngine.save(special);
209:
210: //add a discount property
211: FxPropertyEdit discount = FxPropertyEdit.createNew("Discount",
212: new FxString("Discount in percent"),
213: new FxString("Enter the customers discount"),
214: FxMultiplicity.MULT_0_1, customer.getACL(),
215: FxDataType.Float).setAutoUniquePropertyName(true);
216: assignmentEngine.createProperty(specialId, discount, "/");
217: }
218:
219: /**
220: * Create a relation between Customers, Software and Hardware
221: *
222: * @throws FxApplicationException on errors
223: */
224: public void relation() throws FxApplicationException {
225: TypeEngine typeEngine = EJBLookup.getTypeEngine();
226:
227: FxType customer = CacheAdmin.getEnvironment()
228: .getType(BASE_TYPE);
229: FxType software = CacheAdmin.getEnvironment().getType(
230: "Software");
231: FxType hardware = CacheAdmin.getEnvironment().getType(
232: "Hardware");
233:
234: FxString relDesc = new FxString("Relation Customer<->Products");
235: ACL relationACL = CacheAdmin.getEnvironment().getACL(
236: ACL.Category.STRUCTURE.getDefaultId());
237:
238: FxTypeEdit relEdit = FxTypeEdit.createNew("CustProd", relDesc,
239: relationACL).setMaxRelSource(0).setMaxRelDestination(5)
240: .setMode(TypeMode.Relation).addRelation(
241: FxTypeRelationEdit
242: .createNew(customer, software))
243: .addRelation(
244: FxTypeRelationEdit
245: .createNew(customer, hardware));
246:
247: long typeId = typeEngine.save(relEdit);
248: FxType rel = CacheAdmin.getEnvironment().getType(typeId);
249: }
250:
251: /**
252: * Remove the customer type.
253: * Please note that no relations or references to
254: * customers may exist.
255: *
256: * @throws FxApplicationException on errors
257: */
258: public void deleteCustomer() throws FxApplicationException {
259: TypeEngine typeEngine = EJBLookup.getTypeEngine();
260: ContentEngine contentEngine = EJBLookup.getContentEngine();
261:
262: FxType customer = CacheAdmin.getEnvironment()
263: .getType(BASE_TYPE);
264: contentEngine.removeForType(customer.getId());
265: typeEngine.remove(customer.getId());
266: }
267:
268: //helper methods to make this tutorial actually run
269:
270: /**
271: * Create the needed products - Hardware and Software
272: *
273: * @throws FxApplicationException on errors
274: */
275: public void createProducts() throws FxApplicationException {
276: TypeEngine typeEngine = EJBLookup.getTypeEngine();
277: AssignmentEngine assignmentEngine = EJBLookup
278: .getAssignmentEngine();
279:
280: FxString productDesc = new FxString("Product description ...");
281: ACL productACL = CacheAdmin.getEnvironment().getACL(
282: ACL.Category.STRUCTURE.getDefaultId());
283: //create hard- and software
284: typeEngine.save(FxTypeEdit.createNew("Hardware", productDesc,
285: productACL));
286: typeEngine.save(FxTypeEdit.createNew("Software", productDesc,
287: productACL));
288:
289: //reuse caption as name
290: assignmentEngine.save(FxPropertyAssignmentEdit.reuse(
291: "ROOT/CAPTION", "Hardware", "/", "Name"), false);
292: assignmentEngine.save(FxPropertyAssignmentEdit.reuse(
293: "ROOT/CAPTION", "Software", "/", "Name"), false);
294: }
295:
296: /**
297: * Clean up everything except customer
298: *
299: * @throws FxApplicationException on errors
300: */
301: public void cleanUp() throws FxApplicationException {
302: _clean("CustProd");
303: _clean("Hardware");
304: _clean("Software");
305: _clean("SpecialCustomer");
306: }
307:
308: /**
309: * Remove a type
310: *
311: * @param type name of the type
312: * @throws FxApplicationException on errors
313: */
314: private void _clean(String type) throws FxApplicationException {
315: TypeEngine typeEngine = EJBLookup.getTypeEngine();
316: ContentEngine contentEngine = EJBLookup.getContentEngine();
317:
318: FxType t = CacheAdmin.getEnvironment().getType(type);
319: contentEngine.removeForType(t.getId());
320: typeEngine.remove(t.getId());
321: }
322: }
|