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.models.modelassistant.metabossmodel.codeconstraints;
016:
017: import java.util.ArrayList;
018: import java.util.Arrays;
019: import java.util.Collection;
020: import java.util.Collections;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Set;
025:
026: import javax.jmi.reflect.ConstraintViolationException;
027: import javax.jmi.reflect.RefObject;
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030: import javax.naming.NamingException;
031:
032: import com.metaboss.sdlctools.models.ModelAssistant;
033: import com.metaboss.sdlctools.models.ModelRepository;
034: import com.metaboss.sdlctools.models.ModelRepositoryException;
035: import com.metaboss.sdlctools.models.ModelRepositoryInternalException;
036: import com.metaboss.sdlctools.models.metabossmodel.MetaBossModelPackage;
037: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.EnterpriseModelPackage;
038: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.MessageField;
039: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.MessageFieldClass;
040: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Entity;
041: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.EntityClass;
042: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Selector;
043: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.SystemImplementationModelPackage;
044:
045: /** Implementation of the assistant to MetaBoss models.
046: * Looks after some constraints imposed by Java and particular style of code generation imposed by MetaBoss generators
047: * such as:
048: * <UL>
049: * <LI>Selector name must not match the name of any entity, which is a subtype to the entity, which owns this selector. This is because
050: * MetaBoss generates select<Supertype Entity Plural Name>() method used to narrow collection down to entities of a particular type.</LI>
051: * </UL> */
052: public class ModelAssistantImpl implements ModelAssistant {
053: private static final Set sReservedMessageFieldNames = new HashSet(
054: Arrays.asList(new String[] { "Id", "MessageArguments",
055: "MessageText", "MessageType",
056: "TextResourcePackage", "TextResourceId" }));
057:
058: // This implementation logs absolutely nothing. It is used as default, so
059: // we do not have to check for nulls everywhere in the code
060: private static ModelAssistant.ModelActionsLogger sNullModelActionsLogger = new ModelAssistant.ModelActionsLogger() {
061: public void info(String pModelName, String pMessage) {
062: }
063:
064: public void warn(String pModelName, String pMessage) {
065: }
066:
067: public void error(String pModelName, String pMessage) {
068: }
069: };
070:
071: ModelAssistant.ModelActionsLogger mActionsLogger = sNullModelActionsLogger;
072: // Variables below are cached for whole session with the model regardless of the kind of model it is
073: ModelRepository mModelRepository = null;
074: MetaBossModelPackage mRootPackage = null;
075: EnterpriseModelPackage mEnterpriseModel = null;
076: SystemImplementationModelPackage mSystemImplementationModel = null;
077: EntityClass mEntityClass = null;
078: MessageFieldClass mMessageFieldClass = null;
079:
080: // As per interface
081: public void assignToModel(String pModelName)
082: throws ModelRepositoryException {
083: try {
084: Context lContext = new InitialContext();
085: mModelRepository = (ModelRepository) lContext
086: .lookup(ModelRepository.COMPONENT_URL);
087: mRootPackage = (MetaBossModelPackage) mModelRepository
088: .getModelExtent(pModelName);
089: mEnterpriseModel = mRootPackage.getEnterpriseModel();
090: mMessageFieldClass = mEnterpriseModel.getMessageField();
091: mSystemImplementationModel = mEnterpriseModel
092: .getSystemImplementationModel();
093: mEntityClass = mSystemImplementationModel.getEntity();
094: } catch (NamingException e) {
095: throw new ModelRepositoryInternalException(
096: "Unable to looklup repository", e);
097: }
098: }
099:
100: // As per interface
101: public void dismissFromModel(String pModelName) {
102: mModelRepository = null;
103: mRootPackage = null;
104: mEnterpriseModel = null;
105: mMessageFieldClass = null;
106: mSystemImplementationModel = null;
107: mEntityClass = null;
108: }
109:
110: // Assign the actions loger to this assistantis Assistant to look after a particular model.
111: public void setActionsLogger(String pModelName,
112: ModelAssistant.ModelActionsLogger pModelActionsLogger)
113: throws ModelRepositoryException {
114: mActionsLogger = pModelActionsLogger != null ? pModelActionsLogger
115: : sNullModelActionsLogger;
116: }
117:
118: // As per interface
119: public Collection verifyConstraints(String pModelName) {
120: List lViolations = new ArrayList();
121: // Constaint #1 - check that all selectors have a name not matching a plural name of all owner entity supertypes
122: {
123: for (Iterator lEntitiesIterator = mEntityClass
124: .refAllOfType().iterator(); lEntitiesIterator
125: .hasNext();) {
126: Entity lEntity = (Entity) lEntitiesIterator.next();
127: Collection lSelectors = lEntity.getSelectors();
128: if (!lSelectors.isEmpty()) {
129: Collection lSubtypeEntities = lEntity
130: .getCombinedSubtypes();
131: if (!lSubtypeEntities.isEmpty()) {
132: for (Iterator lSelectorsIterator = lSelectors
133: .iterator(); lSelectorsIterator
134: .hasNext();) {
135: Selector lSelector = (Selector) lSelectorsIterator
136: .next();
137: for (Iterator lSubtypeEntitiesIterator = lSubtypeEntities
138: .iterator(); lSubtypeEntitiesIterator
139: .hasNext();) {
140: Entity lSubtypeEntity = (Entity) lSubtypeEntitiesIterator
141: .next();
142: if (lSelector.getName().equals(
143: lSubtypeEntity.getPluralName())) {
144: lViolations
145: .add(new ConstraintViolationException(
146: lSelector,
147: lSelector
148: .refMetaObject(),
149: "Selector name must not match plural name of any Entity which is a subtype to the Entity which owns the Selector."));
150: lViolations
151: .add(new ConstraintViolationException(
152: lSubtypeEntity,
153: lSubtypeEntity
154: .refMetaObject(),
155: "Entity plural name must not match the name of any selector owned by any Entity which is a supertype to this Entity."));
156: }
157: }
158: }
159: }
160: }
161: }
162: }
163: // Constaint #2 - Message Fields can not have certain names
164: {
165: for (Iterator lMessageFieldsIterator = mMessageFieldClass
166: .refAllOfType().iterator(); lMessageFieldsIterator
167: .hasNext();) {
168: MessageField lMessageField = (MessageField) lMessageFieldsIterator
169: .next();
170: if (sReservedMessageFieldNames.contains(lMessageField
171: .getName())) {
172: lViolations
173: .add(new ConstraintViolationException(
174: lMessageField,
175: lMessageField.refMetaObject(),
176: "Name '"
177: + lMessageField.getName()
178: + "' is reserved and can not be used as the name of the Message Field."));
179: }
180: }
181: }
182: // Return what we have
183: return Collections.unmodifiableCollection(lViolations);
184: }
185:
186: // As per interface
187: public void rectifyModel(String pModelName) {
188: }
189:
190: // As per interface
191: public void onModelElementAttributeBeingUpdated(String pModelName,
192: RefObject pModelElementBeingUpdated, String pAttributeName,
193: Object pOldAttributeValue, Object pNewAttributeValue)
194: throws ModelRepositoryException {
195: // Nothing to do for now
196: }
197:
198: // As per interface
199: public void onModelElementReferenceBeingUpdated(String pModelName,
200: RefObject pModelElementBeingUpdated, String pReferenceName,
201: RefObject pReferencedModelElementToRemove,
202: RefObject pReferencedModelElementToAdd)
203: throws ModelRepositoryException {
204: }
205:
206: // As per interface
207: public void onModelElementBeingDeleted(String pModelName,
208: RefObject pModelElementToBeDeleted)
209: throws ModelRepositoryException {
210: // Nothing to do for now
211: }
212:
213: // As per interface
214: public void onModelElementJustCreated(String pModelName,
215: RefObject pModelElementJustCreated)
216: throws ModelRepositoryException {
217: // Nothing to do for now
218: }
219: }
|