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;
016:
017: import java.util.ArrayList;
018: import java.util.Iterator;
019: import java.util.List;
020:
021: import javax.jmi.reflect.RefObject;
022:
023: import com.metaboss.sdlctools.models.ModelAssistant;
024: import com.metaboss.sdlctools.models.ModelRepositoryException;
025:
026: /** Default implementation of the assistant to MetaBoss models.
027: * This implementation assumes that it is always only serving one model. This is guaranteed by special factory implementation */
028: public abstract class SingleModelAssistantImplBase implements
029: ModelAssistant {
030: // This implementation logs absolutely nothing. It is used as default, so
031: // we do not have to check for nulls everywhere in the code
032: private static ModelAssistant.ModelActionsLogger sNullModelActionsLogger = new ModelAssistant.ModelActionsLogger() {
033: public void info(String pModelName, String pMessage) {
034: }
035:
036: public void warn(String pModelName, String pMessage) {
037: }
038:
039: public void error(String pModelName, String pMessage) {
040: }
041: };
042:
043: private ModelAssistant.ModelActionsLogger mActionsLogger = sNullModelActionsLogger;
044: private String mModelName = null;
045:
046: // As per interface
047: public void assignToModel(String pModelName)
048: throws ModelRepositoryException {
049: mModelName = pModelName;
050: }
051:
052: // As per interface
053: public void dismissFromModel(String pModelName) {
054: mModelName = null;
055: }
056:
057: // Assign the actions loger to this assistantis Assistant to look after a particular model.
058: public void setActionsLogger(String pModelName,
059: ModelAssistant.ModelActionsLogger pModelActionsLogger)
060: throws ModelRepositoryException {
061: mActionsLogger = pModelActionsLogger != null ? pModelActionsLogger
062: : sNullModelActionsLogger;
063: }
064:
065: /** Convenience logging method */
066: public void logInfo(String pMessage) {
067: mActionsLogger.info(mModelName, pMessage);
068: }
069:
070: /** Convenience logging method */
071: public void logWarn(String pMessage) {
072: mActionsLogger.warn(mModelName, pMessage);
073: }
074:
075: /** Convenience logging method */
076: public void logError(String pMessage) {
077: mActionsLogger.error(mModelName, pMessage);
078: }
079:
080: public interface ModelElementLifecycleListener {
081: // Called when corresponding model element has been created
082: public void onElementJustCreated(
083: RefObject pModelElementJustCreated);
084:
085: // Called when corresponding model element about to be deleted
086: public void onElementBeingDeleted(
087: RefObject pModelElementBeingDeleted);
088: }
089:
090: private class ModelElementLifecycleListenerDescriptor {
091: public Class mModelElementClass;
092: public ModelElementLifecycleListener mListener;
093: }
094:
095: private List mLifecycleListeners = new ArrayList();
096:
097: public void addLifecycleListener(Class pModelElementClass,
098: ModelElementLifecycleListener pListener) {
099: ModelElementLifecycleListenerDescriptor lDescriptor = new ModelElementLifecycleListenerDescriptor();
100: lDescriptor.mModelElementClass = pModelElementClass;
101: lDescriptor.mListener = pListener;
102: mLifecycleListeners.add(lDescriptor);
103: }
104:
105: public interface ModelElementAttributeChangeListener {
106: // Called when corresponding attribute has been updated
107: public void onAttributeBeingUpdated(
108: RefObject pModelElementBeingUpdated,
109: String pAttributeName, Object pOldAttributeValue,
110: Object pNewAttributeValue);
111: }
112:
113: private class ModelElementAttributeChangeListenerDescriptor {
114: public Class mModelElementClass;
115: public String mAttributeName;
116: public ModelElementAttributeChangeListener mListener;
117: }
118:
119: private List mAttributeChangeListeners = new ArrayList();
120:
121: public void addAttributeChangeListener(Class pModelElementClass,
122: String pAttributeName,
123: ModelElementAttributeChangeListener pListener) {
124: ModelElementAttributeChangeListenerDescriptor lDescriptor = new ModelElementAttributeChangeListenerDescriptor();
125: lDescriptor.mModelElementClass = pModelElementClass;
126: lDescriptor.mAttributeName = pAttributeName;
127: lDescriptor.mListener = pListener;
128: mAttributeChangeListeners.add(lDescriptor);
129: }
130:
131: public interface ModelElementReferenceChangeListener {
132: // Called when corresponding reference has been updated
133: public void onReferenceBeingUpdated(
134: RefObject pModelElementBeingUpdated,
135: String pReferenceName,
136: RefObject pReferencedModelElementToRemove,
137: RefObject pReferencedModelElementToAdd);
138: }
139:
140: private class ModelElementReferenceChangeListenerDescriptor {
141: public Class mModelElementClass;
142: public String mReferenceName;
143: public ModelElementReferenceChangeListener mListener;
144: }
145:
146: private List mReferenceChangeListeners = new ArrayList();
147:
148: public void addReferenceChangeListener(Class pModelElementClass,
149: String pReferenceName,
150: ModelElementReferenceChangeListener pListener) {
151: ModelElementReferenceChangeListenerDescriptor lDescriptor = new ModelElementReferenceChangeListenerDescriptor();
152: lDescriptor.mModelElementClass = pModelElementClass;
153: lDescriptor.mReferenceName = pReferenceName;
154: lDescriptor.mListener = pListener;
155: mReferenceChangeListeners.add(lDescriptor);
156: }
157:
158: // As per interface
159: public void onModelElementJustCreated(String pModelName,
160: RefObject pModelElementJustCreated)
161: throws ModelRepositoryException {
162: // Dispatch to generic listeners
163: Class pModelElementJustCreatedClass = pModelElementJustCreated
164: .getClass();
165: for (Iterator lLifecycleListenerDescriptorsIterator = mLifecycleListeners
166: .iterator(); lLifecycleListenerDescriptorsIterator
167: .hasNext();) {
168: ModelElementLifecycleListenerDescriptor lDescriptor = (ModelElementLifecycleListenerDescriptor) lLifecycleListenerDescriptorsIterator
169: .next();
170: if (lDescriptor.mModelElementClass
171: .isAssignableFrom(pModelElementJustCreatedClass))
172: lDescriptor.mListener
173: .onElementJustCreated(pModelElementJustCreated);
174: }
175: }
176:
177: // As per interface
178: public void onModelElementAttributeBeingUpdated(String pModelName,
179: RefObject pModelElementBeingUpdated, String pAttributeName,
180: Object pOldAttributeValue, Object pNewAttributeValue)
181: throws ModelRepositoryException {
182: // Dispatch to generic listeners
183: Class pModelElementBeingUpdatedClass = pModelElementBeingUpdated
184: .getClass();
185: for (Iterator lAttributeChangeListenerDescriptorsIterator = mAttributeChangeListeners
186: .iterator(); lAttributeChangeListenerDescriptorsIterator
187: .hasNext();) {
188: ModelElementAttributeChangeListenerDescriptor lDescriptor = (ModelElementAttributeChangeListenerDescriptor) lAttributeChangeListenerDescriptorsIterator
189: .next();
190: if (lDescriptor.mModelElementClass
191: .isAssignableFrom(pModelElementBeingUpdatedClass)
192: && lDescriptor.mAttributeName
193: .equals(pAttributeName))
194: lDescriptor.mListener.onAttributeBeingUpdated(
195: pModelElementBeingUpdated, pAttributeName,
196: pOldAttributeValue, pNewAttributeValue);
197: }
198: }
199:
200: // As per interface
201: public void onModelElementReferenceBeingUpdated(String pModelName,
202: RefObject pModelElementBeingUpdated, String pReferenceName,
203: RefObject pReferencedModelElementToRemove,
204: RefObject pReferencedModelElementToAdd)
205: throws ModelRepositoryException {
206: // Dispatch to generic listeners
207: Class pModelElementBeingUpdatedClass = pModelElementBeingUpdated
208: .getClass();
209: for (Iterator lReferenceChangeListenerDescriptorsIterator = mReferenceChangeListeners
210: .iterator(); lReferenceChangeListenerDescriptorsIterator
211: .hasNext();) {
212: ModelElementReferenceChangeListenerDescriptor lDescriptor = (ModelElementReferenceChangeListenerDescriptor) lReferenceChangeListenerDescriptorsIterator
213: .next();
214: if (lDescriptor.mModelElementClass
215: .isAssignableFrom(pModelElementBeingUpdatedClass)
216: && lDescriptor.mReferenceName
217: .equals(pReferenceName))
218: lDescriptor.mListener.onReferenceBeingUpdated(
219: pModelElementBeingUpdated, pReferenceName,
220: pReferencedModelElementToRemove,
221: pReferencedModelElementToAdd);
222: }
223: }
224:
225: // As per interface
226: public void onModelElementBeingDeleted(String pModelName,
227: RefObject pModelElementToBeDeleted)
228: throws ModelRepositoryException {
229: // Dispatch to generic listeners
230: Class lModelElementToBeDeletedClass = pModelElementToBeDeleted
231: .getClass();
232: for (Iterator lLifecycleListenerDescriptorsIterator = mLifecycleListeners
233: .iterator(); lLifecycleListenerDescriptorsIterator
234: .hasNext();) {
235: ModelElementLifecycleListenerDescriptor lDescriptor = (ModelElementLifecycleListenerDescriptor) lLifecycleListenerDescriptorsIterator
236: .next();
237: if (lDescriptor.mModelElementClass
238: .isAssignableFrom(lModelElementToBeDeletedClass))
239: lDescriptor.mListener
240: .onElementBeingDeleted(pModelElementToBeDeleted);
241: }
242: }
243: }
|