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.implicitselectors;
016:
017: import java.util.ArrayList;
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import javax.jmi.reflect.ConstraintViolationException;
023: import javax.jmi.reflect.RefObject;
024:
025: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.TypeTemplate;
026: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Entity;
027: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Selector;
028: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.SelectorCardinalityEnum;
029: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.SelectorInputField;
030: import com.metaboss.sdlctools.models.metabossmodel.statemachinemodel.StateMachine;
031:
032: /** This class containse helper methods dealing with the Domain Support Servicemodule model element */
033: class TargetEntityStateSelectorHelper {
034: private ModelAssistantImpl mModelAssistantImpl;
035:
036: TargetEntityStateSelectorHelper(
037: ModelAssistantImpl pModelAssistantImpl) {
038: mModelAssistantImpl = pModelAssistantImpl;
039:
040: // Add Entity pluralName attribute change listener
041: mModelAssistantImpl
042: .addAttributeChangeListener(
043: Entity.class,
044: "pluralName",
045: new ModelAssistantImpl.ModelElementAttributeChangeListener() {
046: public void onAttributeBeingUpdated(
047: RefObject pModelElementBeingUpdated,
048: String pAttributeName,
049: Object pOldValue, Object pNewValue) {
050: Entity lEntity = (Entity) pModelElementBeingUpdated;
051: HelperContext lHelperContext = new HelperContext(
052: lEntity);
053: TypeTemplate lEnumerableTypeTemplate = lHelperContext
054: .getEnumerableValueFieldTypeTemplate();
055: if (lEnumerableTypeTemplate == null)
056: return; // Context not bound
057: String lOldEntityPluralName = (String) pOldValue;
058: String lNewEntityPluralName = (String) pNewValue;
059: StateMachine lStateMachine = lEntity
060: .getStateMachine();
061: if (lStateMachine != null) {
062: if (lNewEntityPluralName == null) {
063: // Only old value is known - ensure that the element is deleted
064: if (lOldEntityPluralName != null)
065: ensureAbsent(lEntity,
066: lOldEntityPluralName);
067: } else {
068: // New value is known
069: if (lOldEntityPluralName != null)
070: ensureRenamedPresent(
071: lEntity,
072: lOldEntityPluralName,
073: lNewEntityPluralName);
074: else
075: ensurePresent(lEntity,
076: lNewEntityPluralName);
077: }
078: } else {
079: // State machine does not exists - ensure that the selector is absent
080: if (lOldEntityPluralName != null)
081: ensureAbsent(lEntity,
082: lOldEntityPluralName);
083: if (lNewEntityPluralName != null)
084: ensureAbsent(lEntity,
085: lNewEntityPluralName);
086: }
087: }
088: });
089: // Add StateMachine entity reference change listener
090: mModelAssistantImpl
091: .addReferenceChangeListener(
092: StateMachine.class,
093: "entity",
094: new ModelAssistantImpl.ModelElementReferenceChangeListener() {
095: public void onReferenceBeingUpdated(
096: RefObject pModelElementBeingUpdated,
097: String pReferenceName,
098: RefObject pReferencedModelElementToRemove,
099: RefObject pReferencedModelElementToAdd) {
100: // Ensure that the selector is absent for the entity the state machine is being removed from
101: if (pReferencedModelElementToRemove != null) {
102: Entity lEntity = (Entity) pReferencedModelElementToRemove;
103: String lEntityPluralName = lEntity
104: .getPluralName();
105: if (lEntityPluralName != null)
106: ensureAbsent(lEntity,
107: lEntityPluralName);
108: }
109: // Ensure that the selector is present for the entity the state machine is being addad to
110: if (pReferencedModelElementToAdd != null) {
111: Entity lEntity = (Entity) pReferencedModelElementToAdd;
112: String lEntityPluralName = lEntity
113: .getPluralName();
114: if (lEntityPluralName != null)
115: ensurePresent(lEntity,
116: lEntityPluralName);
117: }
118: }
119: });
120: }
121:
122: // This helper verifies constraints
123: void verifyConstraints(Collection pViolations, Entity pEntity,
124: Collection pUnclaimedSelectors) {
125: String lEntityPluralName = pEntity.getPluralName();
126: if (lEntityPluralName == null)
127: return; // Will not be able to do anything
128: StateMachine lStateMachine = pEntity.getStateMachine();
129: if (lStateMachine != null) {
130: // Work on single value selector
131: String lSingleValueSelectorName = StylesheetImpl
132: .getEntityStateSingleValueSelectorName(lEntityPluralName);
133: Selector lSingleValueSelector = pEntity
134: .findSelector(lSingleValueSelectorName);
135: if (lSingleValueSelector != null) {
136: // Remove this selector from the list of unprocessed ones
137: if (pUnclaimedSelectors != null)
138: pUnclaimedSelectors.remove(lSingleValueSelector);
139: } else
140: pViolations
141: .add(new ConstraintViolationException(
142: pEntity,
143: pEntity.refMetaObject(),
144: "The Entity with the State Machine must have the Single State Selector. Missing '"
145: + lSingleValueSelectorName
146: + "' selector."));
147: // Work on plural value selector
148: String lPluralValueSelectorName = StylesheetImpl
149: .getEntityStatePluralValueSelectorName(lEntityPluralName);
150: Selector lPluralValueSelector = pEntity
151: .findSelector(lPluralValueSelectorName);
152: if (lPluralValueSelector != null) {
153: // Remove this selector from the list of unprocessed ones
154: if (pUnclaimedSelectors != null)
155: pUnclaimedSelectors.remove(lPluralValueSelector);
156: } else
157: pViolations
158: .add(new ConstraintViolationException(
159: pEntity,
160: pEntity.refMetaObject(),
161: "The Entity with the State Machine must have the Plural State Selector. Missing '"
162: + lPluralValueSelectorName
163: + "' selector."));
164: }
165: }
166:
167: // This helper is doing everything necessary to rectify errors reported in verifyConstraints()
168: void rectifyModel(Entity pEntity, Collection pUnclaimedSelectors) {
169: String lEntityPluralName = pEntity.getPluralName();
170: if (lEntityPluralName == null)
171: return; // Will not be able to do anything
172: // Claim selectors which we are managing
173: String lSingleValueSelectorName = StylesheetImpl
174: .getEntityStateSingleValueSelectorName(lEntityPluralName);
175: Selector lSingleValueSelector = pEntity
176: .findSelector(lSingleValueSelectorName);
177: if (lSingleValueSelector != null)
178: pUnclaimedSelectors.remove(lSingleValueSelector);
179: String lPluralValueSelectorName = StylesheetImpl
180: .getEntityStatePluralValueSelectorName(lEntityPluralName);
181: Selector lPluralValueSelector = pEntity
182: .findSelector(lPluralValueSelectorName);
183: if (lPluralValueSelector != null)
184: pUnclaimedSelectors.remove(lPluralValueSelector);
185:
186: StateMachine lStateMachine = pEntity.getStateMachine();
187: if (lStateMachine != null)
188: ensurePresent(pEntity, lEntityPluralName);
189: else
190: ensureAbsent(pEntity, lEntityPluralName);
191: }
192:
193: // This helper renames and ensures that the element is present
194: void ensureRenamedPresent(Entity pEntity,
195: String pOldEntityPluralName, String pNewEntityPluralName) {
196: // Note that this method only deals with renaming and than calls the ensure present method
197:
198: // Work on single value selector attribute
199: String lOldSingleValueSelectorName = StylesheetImpl
200: .getEntityStateSingleValueSelectorName(pOldEntityPluralName);
201: Selector lOldSingleValueSelector = pEntity
202: .findSelector(lOldSingleValueSelectorName);
203: String lNewSingleValueSelectorName = StylesheetImpl
204: .getEntityStateSingleValueSelectorName(pNewEntityPluralName);
205: Selector lNewSingleValueSelector = pEntity
206: .findSelector(lNewSingleValueSelectorName);
207: // Be relaxed here - allow for all sorts of mishaps
208: if (lOldSingleValueSelector != null) {
209: if (lNewSingleValueSelector != null) {
210: // New and old structures are present - just delete the old one
211: lOldSingleValueSelector.refDelete();
212: } else {
213: // Old structure is present - new one is not - rename
214: lOldSingleValueSelector
215: .setName(lNewSingleValueSelectorName);
216: }
217: }
218:
219: // Work on plural value selector attribute
220: String lOldPluralValueSelectorName = StylesheetImpl
221: .getEntityStatePluralValueSelectorName(pOldEntityPluralName);
222: Selector lOldPluralValueSelector = pEntity
223: .findSelector(lOldPluralValueSelectorName);
224: String lNewPluralValueSelectorName = StylesheetImpl
225: .getEntityStatePluralValueSelectorName(pNewEntityPluralName);
226: Selector lNewPluralValueSelector = pEntity
227: .findSelector(lNewPluralValueSelectorName);
228: // Be relaxed here - allow for all sorts of mishaps
229: if (lOldPluralValueSelector != null) {
230: if (lNewPluralValueSelector != null) {
231: // New and old structures are present - just delete the old one
232: lOldPluralValueSelector.refDelete();
233: } else {
234: // Old structure is present - new one is not - rename
235: lOldPluralValueSelector
236: .setName(lNewPluralValueSelectorName);
237: }
238: }
239: // Call the ensure present bit
240: ensurePresent(pEntity, pNewEntityPluralName);
241: }
242:
243: // This helper ensures that the element is present
244: void ensurePresent(Entity pEntity, String pEntityPluralName) {
245: String lStateAttributeName = pEntity
246: .getStateAttributeNameOverride();
247: if (lStateAttributeName == null)
248: lStateAttributeName = "State";
249:
250: // Work on single value selector attribute
251: String lSingleValueSelectorName = StylesheetImpl
252: .getEntityStateSingleValueSelectorName(pEntityPluralName);
253: Selector lSingleValueSelector = pEntity
254: .findSelector(lSingleValueSelectorName);
255: if (lSingleValueSelector == null) {
256: lSingleValueSelector = mModelAssistantImpl.mSelectorClass
257: .createSelector();
258: lSingleValueSelector.setEntity(pEntity);
259: lSingleValueSelector.setName(lSingleValueSelectorName);
260: }
261: lSingleValueSelector
262: .setDescription(StylesheetImpl
263: .getEntityStateSingleValueSelectorDescription(pEntityPluralName));
264: lSingleValueSelector.setImplicit(true);
265: lSingleValueSelector
266: .setOwnerIdentifier(ModelAssistantImpl.ASSISTANT_IDENTIFIER);
267: lSingleValueSelector
268: .setCardinality(SelectorCardinalityEnum.ZERO_TO_MANY);
269: lSingleValueSelector
270: .setTextOfSqlSelect("${EntAttribute."
271: + lStateAttributeName
272: + "} = ${Inputs."
273: + StylesheetImpl
274: .getEntityStateSingleValueSelectorInputFieldName()
275: + "}");
276:
277: // Work on input fields
278: {
279: Collection lInputFields = lSingleValueSelector
280: .getInputFields();
281: // Keep a list of unprocessed fields to be deleted at the end
282: List lUnprocessedInputFields = new ArrayList();
283: lUnprocessedInputFields.addAll(lInputFields);
284: {
285: String lInputFieldName = StylesheetImpl
286: .getEntityStateSingleValueSelectorInputFieldName();
287: SelectorInputField lInputField = lSingleValueSelector
288: .findInputField(lInputFieldName);
289: if (lInputField == null) {
290: lInputField = mModelAssistantImpl.mSelectorInputFieldClass
291: .createSelectorInputField();
292: lInputField.setName(lInputFieldName);
293: lInputFields.add(lInputField);
294: } else {
295: // Remove this input field from the list of unprocessed ones, so it is not deleted
296: lUnprocessedInputFields.remove(lInputField);
297: }
298: lInputField
299: .setDescription(StylesheetImpl
300: .getEnumerableAttributeSingleValueSelectorInputFieldDescription());
301: lInputField.setDataType(pEntity.getStateDataType());
302: lInputField.setArray(false);
303: }
304: // Delete all unprocessed input fields
305: for (Iterator lUnprocessedInputFieldsIterator = lUnprocessedInputFields
306: .iterator(); lUnprocessedInputFieldsIterator
307: .hasNext();) {
308: SelectorInputField lInputField = (SelectorInputField) lUnprocessedInputFieldsIterator
309: .next();
310: lInputField.refDelete();
311: }
312: }
313:
314: // Work on plural value selector attribute
315: String lPluralValueSelectorName = StylesheetImpl
316: .getEntityStatePluralValueSelectorName(pEntityPluralName);
317: Selector lPluralValueSelector = pEntity
318: .findSelector(lPluralValueSelectorName);
319: if (lPluralValueSelector == null) {
320: lPluralValueSelector = mModelAssistantImpl.mSelectorClass
321: .createSelector();
322: lPluralValueSelector.setEntity(pEntity);
323: lPluralValueSelector.setName(lPluralValueSelectorName);
324: }
325: lPluralValueSelector
326: .setDescription(StylesheetImpl
327: .getEntityStatePluralValueSelectorDescription(pEntityPluralName));
328: lPluralValueSelector.setImplicit(true);
329: lPluralValueSelector
330: .setOwnerIdentifier(ModelAssistantImpl.ASSISTANT_IDENTIFIER);
331: lPluralValueSelector
332: .setCardinality(SelectorCardinalityEnum.ZERO_TO_MANY);
333: lPluralValueSelector
334: .setTextOfSqlSelect("${EntAttribute."
335: + lStateAttributeName
336: + "} IN (${Inputs."
337: + StylesheetImpl
338: .getEntityStatePluralValueSelectorInputFieldName()
339: + "})");
340: // Work on input fields
341: {
342: Collection lInputFields = lPluralValueSelector
343: .getInputFields();
344: // Keep a list of unprocessed fields to be deleted at the end
345: List lUnprocessedInputFields = new ArrayList();
346: lUnprocessedInputFields.addAll(lInputFields);
347: {
348: String lInputFieldName = StylesheetImpl
349: .getEntityStatePluralValueSelectorInputFieldName();
350: SelectorInputField lInputField = lPluralValueSelector
351: .findInputField(lInputFieldName);
352: if (lInputField == null) {
353: lInputField = mModelAssistantImpl.mSelectorInputFieldClass
354: .createSelectorInputField();
355: lInputField.setName(lInputFieldName);
356: lInputFields.add(lInputField);
357: } else {
358: // Remove this input field from the list of unprocessed ones, so it is not deleted
359: lUnprocessedInputFields.remove(lInputField);
360: }
361: lInputField
362: .setDescription(StylesheetImpl
363: .getEnumerableAttributePluralValueSelectorInputFieldDescription());
364: lInputField.setDataType(pEntity.getStateDataType());
365: lInputField.setArray(true);
366: }
367: // Delete all unprocessed input fields
368: for (Iterator lUnprocessedInputFieldsIterator = lUnprocessedInputFields
369: .iterator(); lUnprocessedInputFieldsIterator
370: .hasNext();) {
371: SelectorInputField lInputField = (SelectorInputField) lUnprocessedInputFieldsIterator
372: .next();
373: lInputField.refDelete();
374: }
375: }
376: }
377:
378: // This helper makes sure that the single and plural value selectors are absent in the details structure
379: void ensureAbsent(Entity pEntity, String pEntityPluralName) {
380: // Work on single value selector
381: String lSingleValueSelectorName = StylesheetImpl
382: .getEntityStateSingleValueSelectorName(pEntityPluralName);
383: Selector lSingleValueSelector = pEntity
384: .findSelector(lSingleValueSelectorName);
385: if (lSingleValueSelector != null)
386: lSingleValueSelector.refDelete();
387: // Work on plural value selector
388: String lPluralValueSelectorName = StylesheetImpl
389: .getEntityStateSingleValueSelectorName(pEntityPluralName);
390: Selector lPluralValueSelector = pEntity
391: .findSelector(lPluralValueSelectorName);
392: if (lPluralValueSelector != null)
393: lPluralValueSelector.refDelete();
394: }
395: }
|