[gme-users] Java MGA interface :-)

Gabor Szokoli szokoli at ps.uni-sb.de
Wed Apr 7 14:25:17 CDT 2004


Hello!

In order to figure out the misterious attribute retrevial problem I 
asked about, I tried my hands on the java MGA layer, it is really nice 
and clean!

So here's a more precise bug report on attributes not found when they 
should be, one level deeper:

Assume x to be an MgaFCO that has an attribute:

assert(x.getAttributes().getAll().length > 0);

x.getAttributeByName(x.getAttributes().getAll()[0].getMeta().getName())
// throws org.isis.jaut.ComException: Member not found

x.getAttributeByName(x.getAttributes().getAll()[0].getMeta().getName().intern())
// that would have been TOO easy...

x.getAttribute(x.getAttributes().getAll()[0].getMeta())
// same exception


So here's a workaround, a method to be added to JBuilderObject :

public Object getAttributeAsObjectInLinearTime(String name)
    throws NoSuchAttributeException{
    MgaAttribute[] attribs = this.getIObject().getAttributes().getAll();
    for (int i = 0; i < attribs.length; i++) {
        MgaAttribute attribute = attribs[i];
        if (attribute.getMeta().getName().equals(name)) {
            return attribute.getValue();
        }
    }
    throw new NoSuchAttributeException{("Attribute Text missing.");
}


Well, to be honest, my actual workaround is a bit different:
(I always wanted to address a hashtable with a type variable :-) )

public class AttributedBuilderModel extends JBuilderModel {
    //Key: MgaMetaFCO type Value: Hashtable attributeMappings
    static Map attributeTables;
    public AttributedBuilderModel(
        MgaModel arg0,
        JBuilderModel arg1) {
        super(arg0, arg1);
        if (attributeTables == null) {
            attributeTables = new Hashtable();
        }
        //Key: String attributeName Value: Integer index
        Hashtable attributes =
            (Hashtable) attributeTables.get(getMeta());
        if (attributes == null) {
            MgaAttribute[] allAtts = arg0.getAttributes().getAll();
            attributes = new Hashtable(allAtts.length);
            for (int i = 0; i < allAtts.length; i++) {
                MgaAttribute attribute = allAtts[i];
                //Assumes neither the meta, nor the ordering of
                // attributes changes in the lifetime of this
                // object:
                attributes.put(
                    attribute.getMeta().getName(),
                    new Integer(i));
            }
            attributeTables.put(getMeta(), attributes);
        }
    }

    public Object getAttributeAsObject(String name)
        throws NoSuchAttributeException {
        Hashtable attributes =
            (Hashtable) attributeTables.get(getMeta());
        try {
            return getIObject()
                .getAttributes()
                .getAll()[((Integer) attributes.get(name))
                .intValue()]
                .getValue();
        } catch (NullPointerException x) {
            throw new NoSuchAttributeException(
                "Attribute "
                    + name
                    + " was not registered for type "
                    + getIObject().getMeta().getName());
        }
    }
}



More information about the gme-users mailing list