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.content.FxContent;
038: import com.flexive.shared.content.FxPK;
039: import com.flexive.shared.exceptions.FxApplicationException;
040: import com.flexive.shared.interfaces.AssignmentEngine;
041: import com.flexive.shared.interfaces.ContentEngine;
042: import com.flexive.shared.interfaces.TypeEngine;
043: import com.flexive.shared.structure.FxType;
044: import com.flexive.shared.value.FxString;
045: import com.flexive.shared.workflow.Step;
046:
047: /**
048: * Tutorial on how to work with FxContents
049: *
050: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
051: */
052: public class ContentTutorialExample {
053:
054: //Customer FxType
055: public final static String CUSTOMER = "Customer";
056: //Hardware FxType
057: public final static String HARDWARE = "Hardware";
058: //Software FxType
059: public final static String SOFTWARE = "Software";
060: //Relation Customer->Hardware,Software
061: public final static String CUSTOMER_PRODUCT = "CustProd";
062:
063: //ContentEngine reference
064: private ContentEngine ce;
065: //AssignmentEngine reference
066: private AssignmentEngine ae;
067: //TypeEngine reference
068: private TypeEngine te;
069:
070: //used types
071: private FxType customerType, hardwareType, softwareType,
072: relationType;
073:
074: /**
075: * Initialize references to needed 'engines'
076: */
077: public ContentTutorialExample() {
078: this .ce = EJBLookup.getContentEngine();
079: this .ae = EJBLookup.getAssignmentEngine();
080: this .te = EJBLookup.getTypeEngine();
081: this .customerType = CacheAdmin.getEnvironment().getType(
082: CUSTOMER);
083: this .hardwareType = CacheAdmin.getEnvironment().getType(
084: HARDWARE);
085: this .softwareType = CacheAdmin.getEnvironment().getType(
086: SOFTWARE);
087: this .relationType = CacheAdmin.getEnvironment().getType(
088: CUSTOMER_PRODUCT);
089: }
090:
091: /**
092: * Create a new customer instance
093: *
094: * @return primary key of the customer
095: * @throws FxApplicationException on errors
096: */
097: public FxPK createInstance() throws FxApplicationException {
098: FxContent customer = ce.initialize(customerType.getId());
099: customer.setValue("/Name", new FxString(false, "John Doe"));
100: customer.setValue("/BillingAddress/Street", new FxString(false,
101: "Downingstreet 42"));
102: customer.setValue("/BillingAddress/ZIP", new FxString(false,
103: "0815"));
104: return ce.save(customer);
105: }
106:
107: /**
108: * Create a new version for Customer
109: *
110: * @param pk primary key
111: * @return primary key of the new version
112: * @throws FxApplicationException on errors
113: */
114: public FxPK createVersion(FxPK pk) throws FxApplicationException {
115: FxContent customer = ce.load(pk);
116: return ce.createNewVersion(customer);
117: }
118:
119: /**
120: * Change the step of the customer identified by its primary key
121: * to the first step available from his workflow routing.
122: * If no route is available, do nothing.
123: *
124: * @param pk primary key of the customer
125: * @throws FxApplicationException on errors
126: */
127: public void changeWorkflowStep(FxPK pk)
128: throws FxApplicationException {
129: FxContent customer = ce.load(pk);
130: //get the step the customer should be set to
131: //we just retrieve the first available from the customers
132: //valid target steps
133: Step[] targets = EJBLookup.getWorkflowRouteEngine().getTargets(
134: customer.getStepId());
135: if (targets.length > 0) {
136: customer.setStepId(targets[0].getId());
137: ce.save(customer);
138: }
139: }
140:
141: /**
142: * Add a delivery address to a customer
143: *
144: * @param pk primary key
145: * @throws FxApplicationException on errors
146: */
147: public void addDeliveryAddress(FxPK pk)
148: throws FxApplicationException {
149: FxContent customer = ce.load(pk);
150: customer.setValue("/DeliveryAddress/Street", new FxString(
151: false, "Knightsbridge 84"));
152: customer.setValue("/DeliveryAddress/ZIP", new FxString(false,
153: "4711"));
154: ce.save(customer);
155: }
156:
157: /**
158: * Change the delivery address property
159: *
160: * @param pk primary key
161: * @throws FxApplicationException on errors
162: */
163: public void changeDeliveryAddress(FxPK pk)
164: throws FxApplicationException {
165: FxContent customer = ce.load(pk);
166: //variant1 - set a new value
167: customer.setValue("/DeliveryAddress/Street", new FxString(
168: false, "Knightsbridge 85"));
169: //variant2 - update existing value
170: FxString street = (FxString) customer
171: .getValue("/DeliveryAddress/Street");
172: street.setValue("Knightsbridge 85");
173: ce.save(customer);
174: }
175:
176: /**
177: * Create a piece of software or hardware
178: *
179: * @param type either the software or hardware type
180: * @param name name of the product
181: * @return primary key of the product
182: * @throws FxApplicationException on errors
183: */
184: private FxPK createProduct(FxType type, String name)
185: throws FxApplicationException {
186: FxContent product = ce.initialize(type.getId());
187: product.setValue("/Name", new FxString(true, name));
188: return ce.save(product);
189: }
190:
191: /**
192: * Create a relation between a customer and products
193: *
194: * @return primary key of the customer
195: * @throws FxApplicationException on errors
196: */
197: public FxPK relate() throws FxApplicationException {
198: FxPK customer = createInstance();
199: FxPK software = createProduct(softwareType, "[fleXive]");
200: FxPK hardware = createProduct(hardwareType, "Fluxcompensator");
201:
202: FxContent relation = ce.initialize(relationType.getId());
203: relation.setRelatedSource(customer).setRelatedDestination(
204: software);
205: ce.save(relation);
206: relation = ce.initialize(relationType.getId());
207: relation.setRelatedSource(customer).setRelatedDestination(
208: hardware);
209: ce.save(relation);
210: return customer;
211: }
212: }
|