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.Collection;
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: import javax.jmi.reflect.RefObject;
022:
023: import com.metaboss.sdlctools.models.ModelAssistant;
024: import com.metaboss.sdlctools.models.ModelRepositoryException;
025:
026: /** Assistant factory implementation. Special implementation ensures that there
027: * is a dedicated assistant per model and all calls are dispatched to it */
028: public class ModelAssistantFactory implements
029: javax.naming.spi.ObjectFactory {
030: // Map is static in the factory - so there is always only one assistant for a model
031: private static Map sDedicatedAssistantsMap = new HashMap();
032:
033: // Special implementation which dispatches methods to objects dedicated to a model
034: private class DispatchingModelAssistant implements ModelAssistant {
035:
036: public void assignToModel(String pModelName)
037: throws ModelRepositoryException {
038: if (sDedicatedAssistantsMap.containsKey(pModelName))
039: throw new ModelRepositoryException(
040: "Assistant is already assigned to the '"
041: + pModelName + "'model");
042: ModelAssistant lDedicatedModelAssistant = new ModelAssistantImpl();
043: sDedicatedAssistantsMap.put(pModelName,
044: lDedicatedModelAssistant);
045: lDedicatedModelAssistant.assignToModel(pModelName);
046: }
047:
048: public void dismissFromModel(String pModelName)
049: throws ModelRepositoryException {
050: ModelAssistant lDedicatedModelAssistant = (ModelAssistant) sDedicatedAssistantsMap
051: .remove(pModelName);
052: if (lDedicatedModelAssistant != null)
053: lDedicatedModelAssistant.dismissFromModel(pModelName);
054: }
055:
056: public void setActionsLogger(String pModelName,
057: ModelAssistant.ModelActionsLogger pModelActionsLogger)
058: throws ModelRepositoryException {
059: ModelAssistant lDedicatedModelAssistant = (ModelAssistant) sDedicatedAssistantsMap
060: .remove(pModelName);
061: if (lDedicatedModelAssistant != null)
062: lDedicatedModelAssistant.setActionsLogger(pModelName,
063: pModelActionsLogger);
064: }
065:
066: public Collection verifyConstraints(String pModelName)
067: throws ModelRepositoryException {
068: ModelAssistant lDedicatedModelAssistant = (ModelAssistant) sDedicatedAssistantsMap
069: .get(pModelName);
070: if (lDedicatedModelAssistant == null)
071: throw new ModelRepositoryException(
072: "Assistant is not assigned to the '"
073: + pModelName + "'model");
074: return lDedicatedModelAssistant
075: .verifyConstraints(pModelName);
076: }
077:
078: public void rectifyModel(String pModelName)
079: throws ModelRepositoryException {
080: ModelAssistant lDedicatedModelAssistant = (ModelAssistant) sDedicatedAssistantsMap
081: .get(pModelName);
082: if (lDedicatedModelAssistant == null)
083: throw new ModelRepositoryException(
084: "Assistant is not assigned to the '"
085: + pModelName + "'model");
086: lDedicatedModelAssistant.rectifyModel(pModelName);
087: }
088:
089: public void onModelElementBeingDeleted(String pModelName,
090: RefObject pModelElementToBeDeleted)
091: throws ModelRepositoryException {
092: ModelAssistant lDedicatedModelAssistant = (ModelAssistant) sDedicatedAssistantsMap
093: .get(pModelName);
094: if (lDedicatedModelAssistant == null)
095: throw new ModelRepositoryException(
096: "Assistant is not assigned to the '"
097: + pModelName + "'model");
098: lDedicatedModelAssistant.onModelElementBeingDeleted(
099: pModelName, pModelElementToBeDeleted);
100: }
101:
102: public void onModelElementJustCreated(String pModelName,
103: RefObject pModelElementJustCreated)
104: throws ModelRepositoryException {
105: ModelAssistant lDedicatedModelAssistant = (ModelAssistant) sDedicatedAssistantsMap
106: .get(pModelName);
107: if (lDedicatedModelAssistant == null)
108: throw new ModelRepositoryException(
109: "Assistant is not assigned to the '"
110: + pModelName + "'model");
111: lDedicatedModelAssistant.onModelElementJustCreated(
112: pModelName, pModelElementJustCreated);
113: }
114:
115: public void onModelElementAttributeBeingUpdated(
116: String pModelName, RefObject pModelElementBeingUpdated,
117: String pAttributeName, Object pOldAttributeValue,
118: Object pNewAttributeValue)
119: throws ModelRepositoryException {
120: ModelAssistant lDedicatedModelAssistant = (ModelAssistant) sDedicatedAssistantsMap
121: .get(pModelName);
122: if (lDedicatedModelAssistant == null)
123: throw new ModelRepositoryException(
124: "Assistant is not assigned to the '"
125: + pModelName + "'model");
126: lDedicatedModelAssistant
127: .onModelElementAttributeBeingUpdated(pModelName,
128: pModelElementBeingUpdated, pAttributeName,
129: pOldAttributeValue, pNewAttributeValue);
130: }
131:
132: public void onModelElementReferenceBeingUpdated(
133: String pModelName, RefObject pModelElementBeingUpdated,
134: String pReferenceName,
135: RefObject pReferencedModelElementToRemove,
136: RefObject pReferencedModelElementToAdd)
137: throws ModelRepositoryException {
138: ModelAssistant lDedicatedModelAssistant = (ModelAssistant) sDedicatedAssistantsMap
139: .get(pModelName);
140: if (lDedicatedModelAssistant == null)
141: throw new ModelRepositoryException(
142: "Assistant is not assigned to the '"
143: + pModelName + "'model");
144: lDedicatedModelAssistant
145: .onModelElementReferenceBeingUpdated(pModelName,
146: pModelElementBeingUpdated, pReferenceName,
147: pReferencedModelElementToRemove,
148: pReferencedModelElementToAdd);
149: }
150:
151: };
152:
153: /* Returns the instance of the dispatching assistant, which will always ensure that
154: * each model has own copy of actual assistant implementation */
155: public Object getObjectInstance(Object obj, javax.naming.Name name,
156: javax.naming.Context nameCtx,
157: java.util.Hashtable environment) throws Exception {
158: return new DispatchingModelAssistant();
159: }
160: }
|