//Source file: H:\\temp\\generated\\modTransf\\model\\jmi\\JmiUml14ModelHelper.java
package modTransf.model.jmi;
import javax.jmi.reflect.RefPackage;
import modTransf.model.extension.ExtensibleModelHelper;
import modTransf.model.extension.SimpleExtensibleModelHelper;
import modTransf.model.extension.PropertyExtensionBase;
import javax.jmi.reflect.RefObject;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import modTransf.model.ModelException;
import modTransf.model.NotFoundException;
import java.util.AbstractCollection;
import javax.jmi.reflect.RefClass;
import modTransf.util.UnmodifiableFilteredCollection;
import modTransf.util.Filter;
/**
* A model Helper for UML14 models in JMI.
* This helper add custom properties like stereotypes and taggedValues.
* Defined methods:
* <li>
* <ul>associations : Collection Get All association referencing the current element.</ul>
* <ul>associatedElements : Collection Get all elements associated to the current element.</ul>
* <ul>stereotypes : Collection Get all stereotype names associated to current element.</ul>
* <ul></ul>
* </li>
*/
public class JmiUml14ModelHelper extends SimpleExtensibleModelHelper implements
ExtensibleModelHelper
{
/**
* JmiUml14ModelHelper
*
* @param refPackage RefPackage
*/
public JmiUml14ModelHelper(RefPackage refPackage)
{
super(new JmiModelHelper(refPackage));
registerProperties();
}
/**
* JmiUml14ModelHelper
*
* @param nativeModel Object
*/
public JmiUml14ModelHelper(Object nativeModel)
{
super(new JmiModelHelper(nativeModel));
registerProperties();
}
/**
* Register the additional properties for this helper.
*/
protected void registerProperties()
{
registerProperty(new StereotypePropertyExtension());
registerProperty(new AssociationsPropertyExtension());
registerProperty(new AssociatedElementPropertyExtension());
}
/**
* Get the specified Stereotype element associated to the bean.
*
* @param object Object
* @param stereotype String
* @return RefObject The Stereotype concept, or null.
*/
protected RefObject getStereotype(Object object, String stereotype)
{
RefObject modelElement = (RefObject)object;
Collection stereotypes = (Collection)modelElement.refGetValue("stereotype");
return lookupStereotype(stereotypes, stereotype);
}
/**
* Lookup for the specified Stereotype in the provided list of stereotype.
*
* @param stereotypes Object
* @param stereotype String
* @return RefObject The Stereotype concept, or null.
*/
protected RefObject lookupStereotype(Collection stereotypes, String stereotype)
{
//System.out.println(" stereotypes " + stereotypes);
Iterator iter = stereotypes.iterator();
while(iter.hasNext())
{
RefObject stereotypeElement = (RefObject)iter.next();
//System.out.println( " stereotype check '" + stereotypeElement.refGetValue("baseClass") + "'" );
if(stereotype.equals(stereotypeElement.refGetValue("name")))
{
return stereotypeElement;
}
} // end loop
return null;
}
/**
* @param object
* @return List
* @roseuid 3F26F5400197
*/
public Collection getStereotypes(Object object)
{
RefObject modelElement = (RefObject)object;
Collection stereotypes = (Collection)modelElement.refGetValue("stereotype");
return new StereotypeNamesCollection(object, stereotypes);
}
/**
* Add the specified stereotype to the specified bean.
* If the bean is already stereotyped with this name, do nothing.
* @param object
* @param stereotype
*/
protected void addStereotype(Object object, String stereotype)
throws ModelException
{
RefObject modelElement = (RefObject)object;
Collection currentStereotypes = (Collection)modelElement.refGetValue("stereotype");
// Check if it already exist as registered to object.
if(lookupStereotype(currentStereotypes, stereotype)!=null)
{
return;
}
// Don't exist, lookup for a Stereotype element declaration, and
// attach it to object.
RefObject newStereotype = lookupStereotypeDeclaration(object, stereotype);
currentStereotypes.add(newStereotype);
}
/**
* Lookup for the stereotype element associated to the object.
* Create it if necessary.
* @param object
* @param stereotype
* @roseuid 3F26F54001BF
*/
protected RefObject lookupStereotypeDeclaration(Object object, String stereotypeName)
throws ModelException
{
RefObject modelElement = (RefObject)object;
// Get factory
RefClass stereotypeFactory = modelElement.refOutermostPackage().refPackage("Core").
refClass("Stereotype");
// Get all Core.Stereotype
Collection registeredStereotypes = stereotypeFactory.refAllOfClass();
// Get the baseClass
String baseClass = (String)modelElement.refClass().refMetaObject().refGetValue("name");
// Lookup for a matching concept
Iterator iter = registeredStereotypes.iterator();
while(iter.hasNext())
{
RefObject stereotypeElement = (RefObject)iter.next();
//System.out.println( " stereotype check '" + stereotypeElement.refGetValue("baseClass") + "'" );
if(stereotypeName.equals(stereotypeElement.refGetValue("name")))
{
Collection baseClasses = (Collection)stereotypeElement.refGetValue("baseClass");
// Also check the baseClass
if(baseClasses.contains(baseClass))
{
return stereotypeElement;
}
}
} // end loop
// Not found, create it and populate it.
RefObject stereotype = null;
try
{
stereotype = (RefObject)createInstance("Core.Stereotype");
}
catch(InstantiationException ex)
{
throw new ModelException(ex);
}
stereotype.refSetValue("name", stereotypeName);
Collection baseClasses = (Collection)stereotype.refGetValue("baseClass");
baseClasses.add(baseClass);
// Attach stereotype to first model, if any
Collection models = getAllOfClass("Model_Management.Model");
if(models.size()>0)
{
stereotype.refSetValue("namespace", models.iterator().next());
}
return stereotype;
}
/**
* Get the value of the tagged value associated to the stereotype.
* @param object The object
* @param stereotype Name of the stereotype to which the value belong
* @param attributeName The name of the stereotype attribute (tagged value)
* @return The value of the stereotype attribute (tagged value).
* @throws ModelException
*/
public Object getStereotypeTaggedValue(Object object, String stereotype,
String attributeName)
throws ModelException
{
RefObject modelElement = getStereotype(object, stereotype);
Collection tagValues = (Collection)modelElement.refGetValue("taggedValue");
Iterator iter = tagValues.iterator();
while(iter.hasNext())
{
RefObject tag = (RefObject)iter.next();
RefObject tagDef = (RefObject)tag.refGetValue("type");
RefObject tagStereotype = (RefObject)tagDef.refGetValue("owner");
if(tagStereotype==null)
{
continue;
}
String tagName = (String)tagDef.refGetValue("tagType");
String sterotypeName = (String)tagStereotype.refGetValue("baseClass");
if(attributeName.equals(tagName)&&stereotype.equals(stereotype))
{
return tag.refGetValue("dataValue");
}
} // end loop
throw new NotFoundException("Can't find tagValue '"+attributeName+"', '"
+stereotype+"'.");
}
/**
*
* Set the value of the tagged value associated to the stereotype.
* @param object The object
* @param stereotype Name of the stereotype to which the value belong
* @param attributeName The name of the stereotype attribute (tagged value)
* @param value The value to be set
* @throws ModelException
*/
public void setStereotypeTaggedValue(Object object, String stereotype, String attributeName,
Object value)
throws ModelException
{
throw new UnsupportedOperationException("Not yet implemented.");
}
/**
* getAssociatedElements
*
* @param object Object
*/
protected Collection getAssociations(Object object)
{
Collection assocs = getUmlAssociations();
return new UnmodifiableFilteredCollection(assocs, new AssociationFilter( (RefObject)object));
}
/**
* getAssociatedElements
*
* @param object Object
*/
protected Collection getAssociatedElements(Object object)
{
Collection assocs = getUmlAssociations();
return new AssociationOtherEndFilteredCollection(assocs,
new AssociationFilter( (RefObject)object));
}
/**
* Get all uml associations.
*
* @return Collection
*/
protected Collection getUmlAssociations()
{
RefPackage container = getOutermostRefPackage();
return container.refPackage("Core").refClass("Association").refAllOfType();
}
/**
* Get the uml outermost package
*
* @return RefPackage
*/
protected RefPackage getOutermostRefPackage()
{
return(RefPackage)getNativeModel();
}
/**
* addAssociatedElement
*
* @param object Object
* @param value String
*/
protected void addAssociation(Object object, String value)
{
throw new UnsupportedOperationException("Not yet implemented.");
}
/**
* addAssociatedElement
*
* @param object Object
* @param value String
*/
protected void addAssociatedElement(Object object, String value)
{
throw new UnsupportedOperationException("Not yet implemented.");
}
/**
* Nested class for Stereotype properties.
*/
public class StereotypePropertyExtension extends PropertyExtensionBase
{
public StereotypePropertyExtension()
{
super("stereotypes", "Core.ModelElement", "java.lang.String");
}
/**
* Get the list of stereotype names for the specified object.
*
* @param object Object
* @return Object
*/
public Object get(Object object)
{
return getStereotypes(object);
}
/**
* Mark the specified bean as stereotyped.
* Add a stereotype to the specified bean.
*
* @param object Object
* @param value Object
*/
public void set(Object object, Object value)
throws ModelException
{
//System.out.println("addStereotype '" + value + "'");
addStereotype(object, (String)value);
}
}
/**
* A view list on a collection of Core.Stereotype
*/
public class StereotypeNamesCollection extends AbstractCollection
{
/**
* The underlying collection of Core.Stereotypes
*/
private Collection stereotypes;
private Object bean;
public StereotypeNamesCollection(Object bean, Collection stereotypes)
{
this.stereotypes = stereotypes;
this.bean = bean;
}
/**
* size
*
* @return int
*/
public int size()
{
return stereotypes.size();
}
/**
* iterator
*
* @return Iterator
*/
public Iterator iterator()
{
return new StereotypeNamesIterator(stereotypes.iterator());
}
/**
*
* @param object Object A stereotype as a String
* @return boolean
*/
public boolean add(Object stereotypeName)
{
System.out.println("Add stereotype '"+stereotypeName+"'");
try
{
addStereotype(bean, (String)stereotypeName);
return true;
}
catch(ModelException ex)
{ // fail silently ;-(
ex.printStackTrace();
return false;
}
}
/**
* hashCode
*
* @return int
*/
public int hashCode()
{
return stereotypes.hashCode();
}
/**
* equals
*
* @param o Object
* @return boolean
*/
public boolean equals(Object o)
{
return stereotypes.equals(o);
}
}
/**
* Iterator view on a collection of
*/
protected class StereotypeNamesIterator implements Iterator
{
Iterator namesIter;
public StereotypeNamesIterator(Iterator namesIter)
{
this.namesIter = namesIter;
}
/**
* remove
*/
public void remove()
{
namesIter.remove();
}
/**
* hasNext
*
* @return boolean
*/
public boolean hasNext()
{
return namesIter.hasNext();
}
/**
* next
*
* @return Object
*/
public Object next()
{
RefObject stereotype = (RefObject)namesIter.next();
return stereotype.refGetValue("name");
}
}
/**
* Nested class for Stereotype properties.
*/
public class AssociatedElementPropertyExtension extends PropertyExtensionBase
{
public AssociatedElementPropertyExtension()
{
super("associatedElements", "Core.ModelElement", "java.util.Collection");
}
/**
* Get the list of associatedElements specified object.
*
* @param object Object
* @return Object
*/
public Object get(Object object)
{
return getAssociatedElements(object);
}
/**
* Mark the specified bean as stereotyped.
* Add a stereotype to the specified bean.
*
* @param object Object
* @param value Object
*/
public void set(Object object, Object value)
throws ModelException
{
System.out.println("addAssociatedElement '"+value+"'");
addAssociatedElement(object, (String)value);
}
}
/**
* Nested class for Stereotype properties.
*/
public class AssociationsPropertyExtension extends PropertyExtensionBase
{
public AssociationsPropertyExtension()
{
super("associations", "Core.ModelElement", "java.util.Collection");
}
/**
* Get the list of associatedElements specified object.
*
* @param object Object
* @return Object
*/
public Object get(Object object)
{
return getAssociations(object);
}
/**
* Mark the specified bean as stereotyped.
* Add a stereotype to the specified bean.
*
* @param object Object
* @param value Object
*/
public void set(Object object, Object value)
throws ModelException
{
addAssociation(object, (String)value);
}
}
/**
* A filter filtering association having at least one end referencing the
* specified object.
* <p>Titre : ModTransf V3</p>
* <p>Description : </p>
* <p>Copyright : Copyright (c) 2005</p>
* <p>Soci�t� : </p>
* @author Cedric Dumoulin
* @version 3.0
*/
protected class AssociationFilter implements Filter
{
private RefObject firstEnd;
/**
*
* @param firstEnd RefObject
*/
AssociationFilter(RefObject firstEnd)
{
this.firstEnd = firstEnd;
}
/**
* isAllowed
*
* @param object Object
* @return boolean
*/
public boolean isAllowed(Object object)
{
RefObject assoc = (RefObject)object;
List connections = (List)assoc.refGetValue("connection");
Iterator iter = connections.iterator();
while(iter.hasNext())
{
// Get the AssociationEnd
RefObject end = (RefObject)iter.next();
if(firstEnd==end.refGetValue("participant"))
{
return true;
}
}
// nothing found
return false;
}
/**
* Return the other end of the association.
* @param object Object
* @return Object
*/
public Object returnedValue(Object object)
{
RefObject assoc = (RefObject)object;
List connections = (List)assoc.refGetValue("connection");
Iterator iter = connections.iterator();
while(iter.hasNext())
{
// Get the AssociationEnd
RefObject end = (RefObject)((RefObject)iter.next()).refGetValue("participant");
if(firstEnd != end)
{
return end;
}
}
System.out.println("Association other end not found");
// nothing found
return null;
}
}
protected class AssociationOtherEndFilteredCollection extends UnmodifiableFilteredCollection
{
AssociationOtherEndFilteredCollection(Collection coll, AssociationFilter filter)
{
super(coll, filter);
}
/**
* Return the other participant of the association rather than the association.
* @param object Object
* @return Object
*/
protected Object returnedValue(Object object)
{
return( (AssociationFilter)getFilter()).returnedValue(object);
}
}
}