velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cbris...@apache.org
Subject svn commit: r1857757 - in /velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model: ActiveInstanceReference.java EntityReference.java InstanceReference.java ModelTool.java
Date Thu, 18 Apr 2019 15:07:50 GMT
Author: cbrisson
Date: Thu Apr 18 15:07:50 2019
New Revision: 1857757

URL: http://svn.apache.org/viewvc?rev=1857757&view=rev
Log:
[tools/model] Initial import: Velocity context wrappers for all model objects

Added:
    velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/ActiveInstanceReference.java
    velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/EntityReference.java
    velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/InstanceReference.java
    velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/ModelTool.java

Added: velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/ActiveInstanceReference.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/ActiveInstanceReference.java?rev=1857757&view=auto
==============================================================================
--- velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/ActiveInstanceReference.java
(added)
+++ velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/ActiveInstanceReference.java
Thu Apr 18 15:07:50 2019
@@ -0,0 +1,67 @@
+package org.apache.velocity.tools.model;
+
+import java.io.Serializable;
+import java.util.Map;
+
+public class ActiveInstanceReference extends InstanceReference
+{
+    public ActiveInstanceReference(Instance instance)
+    {
+        super(instance);
+    }
+
+    @Override
+    public Serializable put(String key, Serializable value)
+    {
+        return super.putImpl(key, value);
+    }
+
+    @Override
+    public Serializable remove(Object key)
+    {
+        return super.removeImpl(key);
+    }
+
+    @Override
+    public void putAll(Map<? extends String, ? extends Serializable> m)
+    {
+        super.putAllImpl(m);
+    }
+
+    @Override
+    public void clear()
+    {
+        super.clearImpl();
+    }
+
+    @Override
+    public int perform(String name, Map params)
+    {
+        return super.performImpl(name, params);
+    }
+
+    @Override
+    public int perform(String name, Serializable... params)
+    {
+        return super.performImpl(name, params);
+    }
+
+    @Override
+    public boolean delete()
+    {
+        return super.deleteImpl();
+    }
+
+    @Override
+    public boolean insert()
+    {
+        return super.insertImpl();
+    }
+
+    @Override
+    public boolean update()
+    {
+        return super.updateImpl();
+    }
+
+}

Added: velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/EntityReference.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/EntityReference.java?rev=1857757&view=auto
==============================================================================
--- velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/EntityReference.java
(added)
+++ velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/EntityReference.java
Thu Apr 18 15:07:50 2019
@@ -0,0 +1,308 @@
+package org.apache.velocity.tools.model;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.Serializable;
+import java.sql.SQLException;
+import java.util.Iterator;
+import java.util.Map;
+
+public class EntityReference
+{
+    public EntityReference(Entity entity)
+    {
+        this.entity = entity;
+    }
+
+    protected Entity getEntity()
+    {
+        return entity;
+    }
+
+    public Iterator<InstanceReference> iterator()
+    {
+        try
+        {
+            return new ModelTool.InstanceReferenceIterator(entity.iterate());
+        }
+        catch (SQLException sqle)
+        {
+            entity.getLogger().error("cannot iterate on instances of " + entity.getName(),
sqle);
+            return null;
+        }
+    }
+
+    public long getCount()
+    {
+        try
+        {
+            return entity.getCount();
+        }
+        catch (SQLException sqle)
+        {
+            entity.getLogger().error("could not get {} count", entity.getName(), sqle);
+            return 0;
+        }
+    }
+
+    protected InstanceReference createInstanceReference(Instance instance)
+    {
+        switch (getEntity().getModel().getWriteAccess())
+        {
+            case NONE:
+            case JAVA:
+                return new InstanceReference(instance);
+            case VTL:
+                return new ActiveInstanceReference(instance);
+            default:
+                getEntity().getModel().getLogger().error("unhandled write-access enum: {}",
getEntity().getModel().getWriteAccess());
+                return null;
+        }
+    }
+
+    public InstanceReference fetch(Serializable... key) throws SQLException
+    {
+        try
+        {
+            Instance instance = entity.fetch(key);
+            return instance == null ? null : new InstanceReference(instance);
+        }
+        catch (SQLException sqle)
+        {
+            entity.getLogger().error("could not fetch instance of {}", entity.getName(),
sqle);
+            return null;
+        }
+    }
+
+    public Object get(String key)
+    {
+        try
+        {
+            Attribute attribute = entity.getAttribute(key);
+            if (attribute != null)
+            {
+                if (attribute instanceof ScalarAttribute)
+                {
+                    return ((ScalarAttribute)attribute).evaluate();
+                }
+                else if (attribute instanceof RowAttribute)
+                {
+                    Instance instance = ((RowAttribute)attribute).retrieve();
+                    return instance == null ? null : new InstanceReference(instance);
+                }
+                else if (attribute instanceof RowsetAttribute)
+                {
+                    return new ModelTool.InstanceReferenceIterator(((RowsetAttribute)attribute).query());
+                }
+            }
+            return null;
+        }
+        catch (SQLException sqle)
+        {
+            entity.getLogger().error("could not get property {}.{}", entity.getName(), key,
sqle);
+            return null;
+        }
+    }
+
+    public Serializable evaluate(String name, Serializable... params)
+    {
+        try
+        {
+            return entity.evaluate(name, params);
+        }
+        catch (SQLException sqle)
+        {
+            entity.getLogger().error("could not evaluate property {}.{}", entity.getName(),
name, sqle);
+            return null;
+        }
+    }
+
+    public Serializable evaluate(String name, Map params)
+    {
+        try
+        {
+            return entity.evaluate(name, params);
+        }
+        catch (SQLException sqle)
+        {
+            entity.getLogger().error("could not evaluate property {}.{}", entity.getName(),
name, sqle);
+            return null;
+        }
+    }
+
+    public Serializable evaluate(String name)
+    {
+        try
+        {
+            return entity.evaluate(name);
+        }
+        catch (SQLException sqle)
+        {
+            entity.getLogger().error("could not evaluate property {}.{}", entity.getName(),
name, sqle);
+            return null;
+        }
+    }
+
+    public InstanceReference retrieve(String name, Serializable... params)
+    {
+        try
+        {
+            Instance instance = entity.retrieve(name, params);
+            return instance == null ? null : new InstanceReference(instance);
+        }
+        catch (SQLException sqle)
+        {
+            entity.getLogger().error("could not retrieve property {}.{}", entity.getName(),
name, sqle);
+            return null;
+        }
+    }
+
+    public InstanceReference retrieve(String name, Map params)
+    {
+        try
+        {
+            Instance instance = entity.retrieve(name, params);
+            return instance == null ? null : new InstanceReference(instance);
+        }
+        catch (SQLException sqle)
+        {
+            entity.getLogger().error("could not retrieve property {}.{}", entity.getName(),
name, sqle);
+            return null;
+        }
+    }
+
+    public InstanceReference retrieve(String name)
+    {
+        try
+        {
+            Instance instance = entity.retrieve(name);
+            return instance == null ? null : new InstanceReference(instance);
+        }
+        catch (SQLException sqle)
+        {
+            entity.getLogger().error("could not retrieve property {}.{}", entity.getName(),
name, sqle);
+            return null;
+        }
+    }
+
+    public Iterator<InstanceReference> query(String name, Serializable... params)
+    {
+        try
+        {
+            return new ModelTool.InstanceReferenceIterator(entity.query(name, params));
+
+        }
+        catch (SQLException sqle)
+        {
+            entity.getLogger().error("could not query property {}.{}", entity.getName(),
name, sqle);
+            return null;
+        }
+    }
+
+    public Iterator<InstanceReference> query(String name, Map params)
+    {
+        try
+        {
+            return new ModelTool.InstanceReferenceIterator(entity.query(name, params));
+
+        }
+        catch (SQLException sqle)
+        {
+            entity.getLogger().error("could not query property {}.{}", entity.getName(),
name, sqle);
+            return null;
+        }
+    }
+
+    public Iterator<InstanceReference> query(String name)
+    {
+        try
+        {
+            return new ModelTool.InstanceReferenceIterator(entity.query(name));
+
+        }
+        catch (SQLException sqle)
+        {
+            entity.getLogger().error("could not query property {}.{}", entity.getName(),
name, sqle);
+            return null;
+        }
+    }
+
+    public Object get(String key, Map params)
+    {
+        try
+        {
+            Attribute attribute = entity.getAttribute(key);
+            if (attribute != null)
+            {
+                if (attribute instanceof ScalarAttribute)
+                {
+                    return ((ScalarAttribute)attribute).evaluate(params);
+                }
+                else if (attribute instanceof RowAttribute)
+                {
+                    Instance instance = ((RowAttribute)attribute).retrieve(params);
+                    return instance == null ? null : new InstanceReference(instance);
+                }
+                else if (attribute instanceof RowsetAttribute)
+                {
+                    return new ModelTool.InstanceReferenceIterator(((RowsetAttribute)attribute).query(params));
+                }
+            }
+            return null;
+        }
+        catch (SQLException sqle)
+        {
+            entity.getLogger().error("could not get property {}.{}", entity.getName(), key,
sqle);
+            return null;
+        }
+    }
+
+    public Object get(String key, Serializable... params)
+    {
+        try
+        {
+            Attribute attribute = entity.getAttribute(key);
+            if (attribute != null)
+            {
+                if (attribute instanceof ScalarAttribute)
+                {
+                    return ((ScalarAttribute)attribute).evaluate(params);
+                }
+                else if (attribute instanceof RowAttribute)
+                {
+                    Instance instance = ((RowAttribute)attribute).retrieve(params);
+                    return instance == null ? null : new InstanceReference(instance);
+                }
+                else if (attribute instanceof RowsetAttribute)
+                {
+                    return new ModelTool.InstanceReferenceIterator(((RowsetAttribute)attribute).query(params));
+                }
+            }
+            return null;
+        }
+        catch (SQLException sqle)
+        {
+            entity.getLogger().error("could not get property {}.{}", entity.getName(), key,
sqle);
+            return null;
+        }
+    }
+
+    private Entity entity = null;
+}

Added: velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/InstanceReference.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/InstanceReference.java?rev=1857757&view=auto
==============================================================================
--- velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/InstanceReference.java
(added)
+++ velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/InstanceReference.java
Thu Apr 18 15:07:50 2019
@@ -0,0 +1,304 @@
+package org.apache.velocity.tools.model;
+
+import org.apache.velocity.tools.model.util.SlotMap;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Serializable;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+public class InstanceReference implements SlotMap
+{
+    protected static Logger logger = LoggerFactory.getLogger(InstanceReference.class);
+
+    public InstanceReference(Instance instance)
+    {
+        this.instance = instance;
+    }
+
+    private Instance instance;
+
+    @Override
+    public int size()
+    {
+        return instance.size();
+    }
+
+    @Override
+    public boolean isEmpty()
+    {
+        return instance.isEmpty();
+    }
+
+    @Override
+    public boolean containsKey(Object key)
+    {
+        return instance.containsKey(key);
+    }
+
+    @Override
+    public boolean containsValue(Object value)
+    {
+        return instance.containsValue(value);
+    }
+
+    @Override
+    public Serializable get(Object key)
+    {
+        return instance.get(key);
+    }
+
+    @Override
+    public Serializable put(String key, Serializable value)
+    {
+        logger.error("cannot change read-only instance");
+        return null;
+    }
+
+    protected Serializable putImpl(String key, Serializable value)
+    {
+        return instance.put(key, value);
+    }
+
+    @Override
+    public Serializable remove(Object key)
+    {
+        logger.error("cannot change read-only instance");
+        return null;
+    }
+
+    protected Serializable removeImpl(Object key)
+    {
+        return instance.remove(key);
+    }
+
+    @Override
+    public void putAll(Map<? extends String, ? extends Serializable> m)
+    {
+        logger.error("cannot change read-only instance");
+    }
+
+    public void putAllImpl(Map<? extends String, ? extends Serializable> m)
+    {
+        instance.putAll(m);
+    }
+
+    @Override
+    public void clear()
+    {
+        logger.error("cannot change read-only instance");
+    }
+
+    public void clearImpl()
+    {
+        instance.clear();
+    }
+
+    @Override
+    public Set<String> keySet()
+    {
+        return instance.keySet();
+    }
+
+    @Override
+    public Collection<Serializable> values()
+    {
+        return instance.values();
+    }
+
+    @Override
+    public Set<Entry<String, Serializable>> entrySet()
+    {
+        return instance.entrySet();
+    }
+
+    public Serializable evaluate(String name, Map params)
+    {
+        try
+        {
+            return instance.evaluate(name, params);
+        }
+        catch (SQLException sqle)
+        {
+            logger.error("could not evaluate instance property {}.{}", instance.getEntity().getName(),
name, sqle);
+            return null;
+        }
+    }
+
+    public Serializable evaluate(String name, Serializable... params)
+    {
+        try
+        {
+            return instance.evaluate(name, params);
+        }
+        catch (SQLException sqle)
+        {
+            logger.error("could not evaluate instance property {}.{}", instance.getEntity().getName(),
name, sqle);
+            return null;
+        }
+    }
+
+    public InstanceReference retrieve(String name, Map params)
+    {
+        try
+        {
+            Instance inst = instance.retrieve(name, params);
+            return inst == null ? null : new InstanceReference(inst);
+        }
+        catch (SQLException sqle)
+        {
+            logger.error("could not retrieve instance property {}.{}", instance.getEntity().getName(),
name, sqle);
+            return null;
+        }
+    }
+
+    public InstanceReference retrieve(String name, Serializable... params)
+    {
+        try
+        {
+            Instance inst = instance.retrieve(name, params);
+            return inst == null ? null : new InstanceReference(inst);
+        }
+        catch (SQLException sqle)
+        {
+            logger.error("could not retrieve instance property {}.{}", instance.getEntity().getName(),
name, sqle);
+            return null;
+        }
+    }
+
+    public Iterator<InstanceReference> query(String name, Map params)
+    {
+        try
+        {
+            return new ModelTool.InstanceReferenceIterator(instance.query(name, params));
+        }
+        catch (SQLException sqle)
+        {
+            logger.error("could not query instance property {}.{}", instance.getEntity().getName(),
name, sqle);
+            return null;
+        }
+    }
+
+    public Iterator<InstanceReference> query(String name, Serializable... params)
+    {
+        try
+        {
+            return new ModelTool.InstanceReferenceIterator(instance.query(name, params));
+        }
+        catch (SQLException sqle)
+        {
+            logger.error("could not query instance property {}.{}", instance.getEntity().getName(),
name, sqle);
+            return null;
+        }
+    }
+
+    public int perform(String name, Map params)
+    {
+        logger.error("instance is read-only");
+        return 0;
+    }
+
+    protected int performImpl(String name, Map params)
+    {
+        try
+        {
+            return instance.perform(name, params);
+        }
+        catch (SQLException sqle)
+        {
+            logger.error("could not perform instance action {}.{}", instance.getEntity().getName(),
name, sqle);
+            return 0;
+        }
+
+    }
+
+    public int perform(String name, Serializable... params)
+    {
+        logger.error("instance is read-only");
+        return 0;
+    }
+
+    protected int performImpl(String name, Serializable... params)
+    {
+        try
+        {
+            return instance.perform(name, params);
+        }
+        catch (SQLException sqle)
+        {
+            logger.error("could not perform instance action {}.{}", instance.getEntity().getName(),
name, sqle);
+            return 0;
+        }
+    }
+
+    protected Instance getInstance()
+    {
+        return instance;
+    }
+
+    public boolean delete()
+    {
+        logger.error("cannot delete read-only instance");
+        return false;
+    }
+
+    protected boolean deleteImpl()
+    {
+        try
+        {
+            instance.delete();
+            return true;
+        }
+        catch (SQLException sqle)
+        {
+            logger.error("could not delete instance", sqle);
+            return false;
+        }
+    }
+
+    public boolean insert()
+    {
+        logger.error("cannot insert read-only instance");
+        return false;
+    }
+
+    protected boolean insertImpl()
+    {
+        try
+        {
+            instance.insert();
+            return true;
+        }
+        catch (SQLException sqle)
+        {
+            logger.error("could not insert instance", sqle);
+            return false;
+        }
+    }
+
+    public boolean update()
+    {
+        logger.error("cannot update read-only instance");
+        return false;
+    }
+
+    protected boolean updateImpl()
+    {
+        try
+        {
+            instance.update();
+            return true;
+        }
+        catch (SQLException sqle)
+        {
+            logger.error("could not update instance", sqle);
+            return false;
+        }
+    }
+
+
+}

Added: velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/ModelTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/ModelTool.java?rev=1857757&view=auto
==============================================================================
--- velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/ModelTool.java
(added)
+++ velocity/tools/branches/model/velocity-tools-model/src/main/java/org/apache/velocity/tools/model/ModelTool.java
Thu Apr 18 15:07:50 2019
@@ -0,0 +1,263 @@
+package org.apache.velocity.tools.model;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.velocity.tools.Scope;
+import org.apache.velocity.tools.config.DefaultKey;
+import org.apache.velocity.tools.config.ValidScope;
+import org.apache.velocity.tools.generic.SafeConfig;
+import org.apache.velocity.tools.generic.ValueParser;
+import org.apache.velocity.tools.model.config.ConfigHelper;
+import org.apache.velocity.tools.model.config.Constants;
+
+import java.io.Serializable;
+import java.sql.SQLException;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * <p>ModelTool</p>
+ *
+ * @author Claude Brisson
+ * @version $Revision: $
+ * @since VelocityTools 3.1
+ */
+
+@ValidScope(Scope.APPLICATION)
+@DefaultKey("model")
+public class ModelTool extends SafeConfig implements Constants
+{
+    /**
+     * Configuration entry point. Can only be called once.
+     * @param params configuration values map
+     */
+    public void configure(Map<String, Object> params)
+    {
+        String loggerName = new ConfigHelper(params).getString(MODEL_LOGGER_NAME);
+        if (loggerName != null)
+        {
+            params.put(SafeConfig.LOGGER_NAME_KEY, loggerName);
+        }
+        super.configure(params);
+    }
+
+    @Override
+    protected void configure(ValueParser params)
+    {
+        model = createModel();
+
+        // handles default and global configuration ;
+        // all those values can be changed during initialization
+        // when model path is read
+        model.configure(params);
+        String key = params.getString("key");
+        if (key == null || key.length() == 0)
+        {
+            key = "default";
+        }
+        model.initialize(key, model.getDefinition());
+        canWrite = model.getWriteAccess() == Model.WriteAccess.VTL;
+    }
+
+    protected Model createModel()
+    {
+        return new Model();
+    }
+
+    protected Model getModel()
+    {
+        return model;
+    }
+
+    public Serializable evaluate(String name, Serializable... params)
+    {
+        try
+        {
+            return getModel().evaluate(name, params);
+        }
+        catch (SQLException sqle)
+        {
+            getLog().error("could not evaluate property {}", name, sqle);
+            return null;
+        }
+    }
+
+    public Serializable evaluate(String name, Map params)
+    {
+        try
+        {
+            return getModel().evaluate(name, params);
+        }
+        catch (SQLException sqle)
+        {
+            getLog().error("could not evaluate property {}", name, sqle);
+            return null;
+        }
+    }
+
+    public InstanceReference retrieve(String name, Serializable... params)
+    {
+        try
+        {
+            Instance instance = getModel().retrieve(name, params);
+            return instance == null ? null : new InstanceReference(instance);
+        }
+        catch (SQLException sqle)
+        {
+            getLog().error("could not retrieve property {}", name, sqle);
+            return null;
+        }
+    }
+
+    public InstanceReference retrieve(String name, Map params)
+    {
+        try
+        {
+            Instance instance = getModel().retrieve(name, params);
+            return instance == null ? null : new InstanceReference(instance);
+        }
+        catch (SQLException sqle)
+        {
+            getLog().error("could not retrieve property {}", name, sqle);
+            return null;
+        }
+    }
+
+    public Iterator<InstanceReference> query(String name, Serializable... params)
+    {
+        try
+        {
+            return new InstanceReferenceIterator(getModel().query(name, params));
+        }
+        catch (SQLException sqle)
+        {
+            getLog().error("could not iterate property {}", name, sqle);
+            return null;
+        }
+    }
+
+    public Iterator<InstanceReference> query(String name, Map params)
+    {
+        try
+        {
+            return new InstanceReferenceIterator(getModel().query(name, params));
+        }
+        catch (SQLException sqle)
+        {
+            getLog().error("could not iterate property {}", name, sqle);
+            return null;
+        }
+    }
+
+    public int perform(String name, Serializable... params)
+    {
+        try
+        {
+            if (!canWrite)
+            {
+                throw new SQLException("instance is read-only");
+            }
+            return getModel().perform(name, params);
+
+        }
+        catch (SQLException sqle)
+        {
+            getLog().error("could not perform action {}", name, sqle);
+            return 0;
+        }
+    }
+
+    public int perform(String name, Map params)
+    {
+        try
+        {
+            if (!canWrite)
+            {
+                throw new SQLException("instance is read-only");
+            }
+            return getModel().perform(name, params);
+
+        }
+        catch (SQLException sqle)
+        {
+            getLog().error("could not perform action {}", name, sqle);
+            return 0;
+        }
+    }
+
+    public Object get(String key)
+    {
+        try
+        {
+            Attribute attribute = model.getAttribute(key);
+            if (attribute != null)
+            {
+                if (attribute instanceof ScalarAttribute)
+                {
+                    return ((ScalarAttribute)attribute).evaluate();
+                }
+                else if (attribute instanceof RowAttribute)
+                {
+                    Instance instance = ((RowAttribute)attribute).retrieve();
+                    return instance == null ? null : new InstanceReference(instance);
+                }
+                else if (attribute instanceof RowsetAttribute)
+                {
+                    return new InstanceReferenceIterator(((RowsetAttribute)attribute).query());
+                }
+            }
+            Entity entity = model.getEntity(key);
+            if (entity != null)
+            {
+                return new EntityReference(entity);
+            }
+            return null;
+        }
+        catch (SQLException sqle)
+        {
+            getLog().error("could not get property {}", key, sqle);
+            return null;
+        }
+    }
+
+    private Model model = null;
+    private boolean canWrite = false;
+
+    public static class InstanceReferenceIterator implements Iterator<InstanceReference>
+    {
+        public InstanceReferenceIterator(Iterator<Instance> iterator)
+        {
+            this.iterator = iterator;
+        }
+
+        @Override
+        public boolean hasNext()
+        {
+            return iterator.hasNext();
+        }
+
+        @Override
+        public InstanceReference next()
+        {
+            return new InstanceReference(iterator.next());
+        }
+        private Iterator<Instance> iterator;
+    }
+}



Mime
View raw message