001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.sdlctools.applications.examples;
016:
017: import java.io.File;
018: import java.util.Collection;
019: import java.util.Iterator;
020:
021: import javax.naming.Context;
022: import javax.naming.InitialContext;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.log4j.PropertyConfigurator;
027:
028: import com.metaboss.enterprise.BaseException;
029: import com.metaboss.sdlctools.models.ModelRepository;
030: import com.metaboss.sdlctools.models.ModelValidationException;
031: import com.metaboss.sdlctools.models.metabossmodel.MetaBossModelPackage;
032: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Enterprise;
033:
034: /*
035: * Created on 23/10/2003
036: *
037: * To change the template for this generated file go to
038: * Window>Preferences>Java>Code Generation>Code and Comments
039: */
040:
041: /**
042: * @author Rost
043: *
044: * To change the template for this generated type comment go to
045: * Window>Preferences>Java>Code Generation>Code and Comments
046: */
047: public class UpdatingAttributeExample {
048: // Commons Logging instance.
049: private static final Log sLogger;
050: static {
051: // Configure logger as early as possible
052: PropertyConfigurator.configure(System.getProperties());
053: sLogger = LogFactory.getLog(UpdatingAttributeExample.class);
054: }
055:
056: // Initialise enterprise model properties
057: private static File sEnterpriseModelRootFile = new File(
058: ModelProperties.sEnterpriseModelsRootDirectory
059: .getAbsolutePath()
060: + File.separator + "HatMaker\\Model.xml");
061:
062: public static void main(String[] args) {
063: try {
064: ModelRepository lModelRepository = null;
065: try {
066: Context lContext = new InitialContext();
067: lModelRepository = (ModelRepository) lContext
068: .lookup(ModelRepository.COMPONENT_URL);
069: // sLogger.info("Loading MetaBossMetaModel...");
070: // lModelRepository.loadMetaModel("MetaBossMetaModel",ModelProperties.sMetaModelFile,"MetaBossModel",ModelProperties.sDefaultPartitioningProperties);
071: sLogger.info("Opening HatMaker model...");
072: lModelRepository.openModel("Enterprises.HatMaker",
073: sEnterpriseModelRootFile, "MetaBossMetaModel");
074: sLogger.info("Testing if we can do stuff...");
075: MetaBossModelPackage lMetaBossModelPackage = (MetaBossModelPackage) lModelRepository
076: .getModelExtent("Enterprises.HatMaker");
077: lModelRepository.beginTransaction();
078: try {
079: Collection lEnterprises = lMetaBossModelPackage
080: .getEnterpriseModel().getEnterprise()
081: .refAllOfClass();
082: for (Iterator lEnterprisesIterator = lEnterprises
083: .iterator(); lEnterprisesIterator.hasNext();) {
084: Enterprise lEnterprise = (Enterprise) lEnterprisesIterator
085: .next();
086: String lOldDescription = lEnterprise
087: .getDescription();
088: String lNewDescription = "Changed at "
089: + System.currentTimeMillis();
090: sLogger.info("Changing '" + lOldDescription
091: + "' to '" + lNewDescription + "' in "
092: + lEnterprise.getRef());
093: lEnterprise.setDescription(lNewDescription);
094: }
095:
096: lModelRepository.commitTransaction();
097: lModelRepository.saveModel("Enterprises.HatMaker",
098: true);
099: sLogger.info("Done updating the atribute");
100: } finally {
101: if (lModelRepository.isInTransaction())
102: lModelRepository.rollbackTransaction();
103: }
104: } finally {
105: if (lModelRepository != null)
106: lModelRepository.close();
107: }
108: } catch (Throwable t) {
109: handleException(t);
110: }
111: }
112:
113: // This helper must be called from all tools when exception has been caught
114: // it will print the exception in the best possible manner
115: static void handleException(Throwable pThrowable) {
116: System.out.println("Caught exception "
117: + pThrowable.getClass().getName());
118: if (pThrowable instanceof BaseException) {
119: pThrowable.printStackTrace(System.out);
120: Throwable t = pThrowable.getCause();
121: while (t != null) {
122: System.out.println();
123: System.out.println();
124: System.out.println("Cause : " + t.getClass().getName());
125: t.printStackTrace(System.out);
126: // Special case for model validation exception - it has explanation
127: if (t instanceof ModelValidationException) {
128: System.out.println(((ModelValidationException) t)
129: .getFullExplanation());
130: break;
131: } else if (t instanceof BaseException) {
132: t = ((BaseException) t).getCause();
133: } else
134: break;
135: }
136: } else {
137: System.out.println(pThrowable.getMessage());
138: pThrowable.printStackTrace(System.out);
139: }
140: }
141: }
|