Coverage Report - org.argouml.model.mdr.ExtensionMechanismsFactoryMDRImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ExtensionMechanismsFactoryMDRImpl
8%
13/149
1%
1/58
3.043
 
 1  
 /* $Id: ExtensionMechanismsFactoryMDRImpl.java 18760 2010-09-18 05:19:53Z tfmorris $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009,2010 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    Tom Morris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2007 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies.  This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason.  IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.model.mdr;
 40  
 
 41  
 import java.util.Collection;
 42  
 import java.util.Iterator;
 43  
 
 44  
 import org.argouml.model.CoreHelper;
 45  
 import org.argouml.model.ExtensionMechanismsFactory;
 46  
 import org.argouml.model.ExtensionMechanismsHelper;
 47  
 import org.omg.uml.foundation.core.ModelElement;
 48  
 import org.omg.uml.foundation.core.Namespace;
 49  
 import org.omg.uml.foundation.core.Stereotype;
 50  
 import org.omg.uml.foundation.core.TagDefinition;
 51  
 import org.omg.uml.foundation.core.TaggedValue;
 52  
 import org.omg.uml.foundation.datatypes.Multiplicity;
 53  
 
 54  
 /**
 55  
  * Factory to create UML classes for the UML ExtensionMechanisms
 56  
  * package.<p>
 57  
  * 
 58  
  * @since ARGO0.19.5
 59  
  * @author Ludovic Ma&icirc;tre
 60  
  * @author Tom Morris
 61  
  * <p>
 62  
  * derived from NSUML implementation by:
 63  
  * @author Thierry Lach
 64  
  */
 65  14
 class ExtensionMechanismsFactoryMDRImpl extends
 66  
         AbstractUmlModelFactoryMDR implements ExtensionMechanismsFactory {
 67  
 
 68  
     /**
 69  
      * The model implementation.
 70  
      */
 71  
     private MDRModelImplementation modelImpl;
 72  
 
 73  
     /**
 74  
      * The extension mechanism helper.
 75  
      */
 76  
     private ExtensionMechanismsHelper extensionHelper;
 77  
 
 78  
     /**
 79  
      * Don't allow instantiation.
 80  
      * 
 81  
      * @param implementation
 82  
      *            To get other helpers and factories.
 83  
      */
 84  900
     ExtensionMechanismsFactoryMDRImpl(MDRModelImplementation implementation) {
 85  900
         modelImpl = implementation;
 86  900
         extensionHelper = implementation.getExtensionMechanismsHelper();
 87  900
     }
 88  
 
 89  
     /*
 90  
      * @see org.argouml.model.ExtensionMechanismsFactory#createTaggedValue()
 91  
      */
 92  
     public TaggedValue createTaggedValue() {
 93  0
         TaggedValue tv = modelImpl.getUmlPackage().getCore().getTaggedValue().
 94  
                 createTaggedValue();
 95  0
         super.initialize(tv);
 96  0
         return tv;
 97  
     }
 98  
 
 99  
     /**
 100  
      * Get an instance of a UML TagDefinition.
 101  
      * 
 102  
      * @param tagName The name of the TagDefinition to create/retrieve
 103  
      * @return an initialized UML TaggedValue instance.
 104  
      */
 105  
     TagDefinition getTagDefinition(String tagName) {
 106  0
         if (tagName == null) {
 107  0
             throw new IllegalArgumentException("Argument may not be null");
 108  
         }
 109  
         
 110  
         // Look for a TagDefinition matching the given name
 111  0
         for (Iterator i = modelImpl.getUmlPackage().getCore().getTagDefinition()
 112  0
                 .refAllOfClass().iterator(); i.hasNext();) {
 113  0
             TagDefinition td = (TagDefinition) i.next();
 114  0
             if (tagName.equals(td.getName())) {
 115  0
                 return td;
 116  
             }
 117  0
         }
 118  
         
 119  
         // Create a new TagDefinition if none found
 120  0
         Object rootModel = modelImpl.getModelManagementFactory().getRootModel();
 121  0
         TagDefinition td = buildTagDefinition(tagName, null, rootModel);
 122  
 
 123  0
         super.initialize(td);
 124  0
         return td;
 125  
     }
 126  
 
 127  
     /*
 128  
      * TODO: MVW: This needs rethinking/rework! I have the following questions:
 129  
      * Why does it not search for a stereotype in the namespace using properties
 130  
      * and only create a new stereotype if it will actually be used? Ie, why is
 131  
      * there not a getStereotype(String name, String baseClass)? (edited by
 132  
      * d00mst)  <these comments imported from NSUML implementation - tfm>
 133  
      * 
 134  
      * @see org.argouml.model.ExtensionMechanismsFactory#buildStereotype(java.lang.Object, java.lang.Object, java.lang.Object)
 135  
      */
 136  
     public Stereotype buildStereotype(
 137  
             Object theModelElementObject,
 138  
             Object theName,
 139  
             Object theNamespaceObject) {
 140  
         
 141  0
         if (theModelElementObject == null || theName == null
 142  
                 || theNamespaceObject == null) {
 143  0
             throw new IllegalArgumentException(
 144  
                     "one of the arguments is null: modelElement="
 145  
                     + theModelElementObject
 146  
                     + " name=" + theName
 147  
                     + " namespace=" + theNamespaceObject);
 148  
         }
 149  
         
 150  0
         ModelElement me = (ModelElement) theModelElementObject;
 151  
         
 152  0
         String text = (String) theName;
 153  0
         Namespace ns = (Namespace) theNamespaceObject;
 154  0
         Stereotype stereo = buildStereotype(text);
 155  0
         stereo.getBaseClass().add(modelImpl.getMetaTypes().getName(me));
 156  
         // TODO: this doesn't look right - review - tfm
 157  0
         Stereotype stereo2 = (Stereotype) extensionHelper.getStereotype(ns,
 158  
                 stereo);
 159  0
         if (stereo2 != null) {
 160  0
             me.getStereotype().add(stereo2);
 161  0
             modelImpl.getUmlFactory().delete(stereo);
 162  0
             return stereo2;
 163  
         }
 164  0
         stereo.setNamespace(ns);
 165  0
         me.getStereotype().add(stereo);
 166  0
         return stereo;
 167  
     }
 168  
 
 169  
 
 170  
     public Stereotype buildStereotype(
 171  
             Object theModelElementObject,
 172  
             String theName,
 173  
             Object model,
 174  
             Collection models) {
 175  
         
 176  0
         ModelElement me = (ModelElement) theModelElementObject;
 177  
 
 178  0
         Stereotype stereo = buildStereotype(theName);
 179  0
         stereo.getBaseClass().add(modelImpl.getMetaTypes().getName(me));
 180  0
         Stereotype stereo2 = (Stereotype) extensionHelper.getStereotype(models,
 181  
                 stereo);
 182  0
         if (stereo2 != null) {
 183  0
             me.getStereotype().add(stereo2);
 184  0
             modelImpl.getUmlFactory().delete(stereo);
 185  0
             return stereo2;
 186  
         }
 187  0
         stereo.setNamespace((org.omg.uml.modelmanagement.Model) model);
 188  0
         if (me != null) {
 189  0
             me.getStereotype().add(stereo);
 190  
         }
 191  0
         return stereo;
 192  
     }
 193  
 
 194  
     /*
 195  
      * Builds an initialized stereotype with no namespace. A stereotype must
 196  
      * have a namespace so this method is unsafe. Use buildStereotype(String,
 197  
      * Object).
 198  
      * 
 199  
      * @param text
 200  
      *            is the name of the stereotype
 201  
      * @return an initialized stereotype.
 202  
      */
 203  
     private Stereotype buildStereotype(String text) {
 204  0
         Stereotype stereotype = modelImpl.getUmlPackage().getCore().
 205  
                 getStereotype().createStereotype();
 206  0
         super.initialize(stereotype);
 207  0
         stereotype.setName(text);
 208  0
         return stereotype;
 209  
     }
 210  
 
 211  
 
 212  
     public Stereotype buildStereotype(String text, Object namespace) {
 213  14
         if (!(namespace instanceof Namespace)) {
 214  0
             throw new IllegalArgumentException(
 215  
                     "Namespace is wrong type - text:" + text + ",ns:"
 216  
                             + namespace);
 217  
         }
 218  14
         Namespace ns = (Namespace) namespace;
 219  14
         org.omg.uml.UmlPackage umlPkg = ((org.omg.uml.UmlPackage) ns
 220  
                 .refOutermostPackage());
 221  14
         Stereotype stereotype = umlPkg.getCore().getStereotype()
 222  
                 .createStereotype();
 223  14
         super.initialize(stereotype);
 224  14
         stereotype.setName(text);
 225  14
         stereotype.setNamespace(ns);
 226  14
         return stereotype;
 227  
     }
 228  
 
 229  
 
 230  
     @Deprecated
 231  
     public TaggedValue buildTaggedValue(String tag, String value) {
 232  0
         TaggedValue tv = buildTaggedValue(getTagDefinition(tag));
 233  0
         tv.getDataValue().add(value);
 234  0
         return tv;
 235  
     }
 236  
 
 237  
     private TaggedValue buildTaggedValue(TagDefinition type) {
 238  0
         TaggedValue tv = createTaggedValue();
 239  0
         tv.setType(type);
 240  0
         return tv;
 241  
     }
 242  
     
 243  
     public TaggedValue buildTaggedValue(Object type, String[] values) {
 244  0
         if (!(type instanceof TagDefinition)) {
 245  0
             throw new IllegalArgumentException(
 246  
                     "TagDefinition required, received - " + type);
 247  
         }
 248  0
         TaggedValue tv = buildTaggedValue((TagDefinition) type);
 249  0
         for (String value : values) {
 250  0
             tv.getDataValue().add(value);
 251  
         }
 252  0
         return tv;
 253  
     }
 254  
 
 255  
 
 256  
     public void copyTaggedValues(Object source, Object target) {    
 257  0
         if (!(source instanceof ModelElement)
 258  
                 || !(target instanceof ModelElement)) {
 259  0
             throw new IllegalArgumentException();
 260  
         }
 261  
 
 262  0
         Iterator it = ((ModelElement) source).getTaggedValue().iterator();
 263  0
         Collection taggedValues = ((ModelElement) target).getTaggedValue();
 264  
         // Clear target so that multiple copies have no effect 
 265  
         // (other than inefficiency)
 266  0
         taggedValues.clear();
 267  0
         while (it.hasNext()) {
 268  0
             taggedValues.add(copyTaggedValue((TaggedValue) it.next()));
 269  
         }
 270  0
     }
 271  
     
 272  
     /**
 273  
      * Copy a single TaggedValue and return the copy.
 274  
      * 
 275  
      * @param source the TaggedValue to copy
 276  
      * @return the newly cloned copy
 277  
      */
 278  
     private Object copyTaggedValue(TaggedValue source) {
 279  0
         TaggedValue tv = createTaggedValue();
 280  0
         tv.setType(source.getType());
 281  0
         tv.getDataValue().addAll(source.getDataValue());
 282  0
         tv.getReferenceValue().addAll(source.getReferenceValue());
 283  0
         return tv;
 284  
     }
 285  
 
 286  
     /**
 287  
      * @param elem
 288  
      *            the stereotype
 289  
      */
 290  
     void deleteStereotype(Object elem) {
 291  0
         if (!(elem instanceof Stereotype)) {
 292  0
             throw new IllegalArgumentException();
 293  
         }
 294  0
         modelImpl.getUmlHelper().deleteCollection(
 295  
                 ((Stereotype) elem).getDefinedTag());
 296  0
         modelImpl.getUmlHelper().deleteCollection(
 297  
                 ((Stereotype) elem).getStereotypeConstraint());
 298  0
     }
 299  
 
 300  
     /**
 301  
      * @param elem
 302  
      *            the taggedvalue
 303  
      */
 304  
     void deleteTaggedValue(Object elem) {
 305  0
         if (!(elem instanceof TaggedValue)) {
 306  0
             throw new IllegalArgumentException();
 307  
         }
 308  0
     }
 309  
 
 310  
     /**
 311  
      * Delete a TagDefinition.
 312  
      * 
 313  
      * @param elem the element to be deleted
 314  
      */
 315  
     void deleteTagDefinition(Object elem) {
 316  0
         if (!(elem instanceof TagDefinition)) {
 317  0
             throw new IllegalArgumentException();
 318  
         }
 319  
         // Delete all TaggedValues with this type
 320  0
         TagDefinition td = (TagDefinition) elem;
 321  0
         modelImpl.getUmlHelper().deleteCollection(
 322  
                 ((org.omg.uml.UmlPackage) td.refOutermostPackage()).getCore()
 323  
                         .getATypeTypedValue().getTypedValue(td));
 324  0
     }
 325  
 
 326  
 
 327  
     public Object copyStereotype(Object source, Object ns) {
 328  0
         if (!(source instanceof Stereotype)) {
 329  0
             throw new IllegalArgumentException("source");
 330  
         }
 331  0
         if (!(ns instanceof Namespace)) {
 332  0
             throw new IllegalArgumentException("namespace");
 333  
         }
 334  
 
 335  0
         Stereotype st = buildStereotype(null, ns);
 336  0
         doCopyStereotype((Stereotype) source, st);
 337  0
         return st;
 338  
     }
 339  
 
 340  
     /*
 341  
      * Used by the copy functions. Do not call this function directly.
 342  
      * 
 343  
      * @param source
 344  
      *            The stereotype to copy from.
 345  
      * @param target
 346  
      *            The object becoming a copy.
 347  
      */
 348  
     private void doCopyStereotype(Stereotype source, Stereotype target) {
 349  0
         ((CoreFactoryMDRImpl) modelImpl.getCoreFactory())
 350  
                 .doCopyGeneralizableElement(source, target);
 351  0
         target.getBaseClass().clear();
 352  0
         target.getBaseClass().addAll(source.getBaseClass());
 353  0
         target.setIcon(source.getIcon());
 354  
         // TODO: constraints
 355  
         // TODO: required tags
 356  0
     }
 357  
 
 358  
     public TagDefinition buildTagDefinition(String name, Object owner, 
 359  
             Object namespace) {
 360  0
         return buildTagDefinition(name, owner, namespace, null); // "Element");
 361  
     }
 362  
 
 363  
     public TagDefinition buildTagDefinition(String name, Object owner, 
 364  
             Object ns, String tagType) {
 365  0
         if (owner != null) {
 366  0
             if (!(owner instanceof Stereotype)) {
 367  0
                 throw new IllegalArgumentException("owner: " + owner);
 368  
             }
 369  0
             if (ns != null) {
 370  0
                 throw new IllegalArgumentException(
 371  
                         "only one of owner & namespace may be specified");
 372  
             }
 373  0
         } else if (!(ns instanceof Namespace)) {
 374  0
             throw new IllegalArgumentException("namespace: " + ns);
 375  
         }
 376  0
         TagDefinition td = (TagDefinition) createTagDefinition();
 377  0
         CoreHelper coreHelper = org.argouml.model.Model.getCoreHelper();
 378  0
         if (owner != null) {
 379  0
             coreHelper.setOwner(td, owner);
 380  
         } else {
 381  0
             coreHelper.setNamespace(td, ns);
 382  
         }
 383  0
         coreHelper.setName(td, name);
 384  0
         coreHelper.setMultiplicity(td, 0, 1);
 385  0
         td.setTagType(tagType);
 386  0
         return td;
 387  
     }
 388  
 
 389  
 
 390  
     public Object createTagDefinition() {
 391  0
         TagDefinition td = modelImpl.getUmlPackage().getCore()
 392  
                 .getTagDefinition().createTagDefinition();
 393  0
         super.initialize(td);
 394  0
         return td;
 395  
     }
 396  
     
 397  
 
 398  
     public Object createStereotype() {
 399  0
         Stereotype st = modelImpl.getUmlPackage().getCore().getStereotype()
 400  
             .createStereotype();
 401  0
         super.initialize(st);
 402  0
         return st;
 403  
     }
 404  
 
 405  
 
 406  
     public Object copyTagDefinition(Object anElement, Object aNs) {
 407  0
         if (!(anElement instanceof TagDefinition)) {
 408  0
             throw new IllegalArgumentException("source: " + anElement);
 409  
         }
 410  0
         if (!(aNs instanceof Namespace || aNs instanceof Stereotype)) {
 411  0
             throw new IllegalArgumentException("namespace: " + aNs);
 412  
         }
 413  0
         TagDefinition source = (TagDefinition) anElement;
 414  0
         TagDefinition td = (TagDefinition) createTagDefinition();
 415  0
         if (aNs instanceof Namespace) {
 416  0
             td.setNamespace((Namespace) aNs);
 417  
         } else {
 418  0
             td.setOwner((Stereotype) aNs);
 419  
         }
 420  0
         doCopyTagDefinition(source, td);
 421  0
         return td;
 422  
     }
 423  
     
 424  
     /*
 425  
      * Used by the copy functions. Do not call this function directly.
 426  
      * 
 427  
      * @param source
 428  
      *            The stereotype to copy from.
 429  
      * @param target
 430  
      *            The object becoming a copy.
 431  
      */
 432  
     private void doCopyTagDefinition(TagDefinition source, 
 433  
             TagDefinition target) {
 434  0
         ((CoreFactoryMDRImpl) modelImpl.getCoreFactory())
 435  
                 .doCopyModelElement(source, target);
 436  0
         target.setTagType(source.getTagType());
 437  0
         String srcMult = org.argouml.model.Model.getFacade().toString(
 438  
                 source.getMultiplicity());
 439  0
         target.setMultiplicity(modelImpl.getDataTypesFactoryInternal()
 440  
                 .createMultiplicityInternal(srcMult));
 441  0
     }    
 442  
 }