Queries are implemented, and appear to work as expected. Added getKeys() method

to AttributeService.  Fixed NPE in MapAttributeImpl.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@5514 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Britt Park
2007-04-19 19:24:49 +00:00
parent 8eb616f446
commit 65ddab9b8c
14 changed files with 223 additions and 29 deletions

View File

@@ -28,6 +28,7 @@ package org.alfresco.repo.attributes;
import java.util.List;
import org.alfresco.service.cmr.attributes.AttrQuery;
import org.alfresco.util.Pair;
/**
* Interface for persistence operations on attributes.
@@ -49,10 +50,9 @@ public interface AttributeDAO
/**
* Find all attributes that match a given path and AttrQuery.
* @param path The path, starting from the top to the map in which to
* search for matching attributes.
* @param map The map within which to query.
* @param query The AttrQuery.
* @return A List of Attributes.
* @return A List of key, attribute value pairs.
*/
List<Attribute> find(String path, AttrQuery query);
List<Pair<String,Attribute>> find(MapAttribute map, AttrQuery query);
}

View File

@@ -34,6 +34,7 @@ import org.alfresco.service.cmr.attributes.AttributeService;
import org.alfresco.service.cmr.avm.AVMBadArgumentException;
import org.alfresco.service.cmr.avm.AVMNotFoundException;
import org.alfresco.service.cmr.avm.AVMWrongTypeException;
import org.alfresco.util.Pair;
/**
* Implementation of the AttributeService interface.
@@ -43,6 +44,8 @@ public class AttributeServiceImpl implements AttributeService
{
private GlobalAttributeEntryDAO fGlobalAttributeEntryDAO;
private AttributeDAO fAttributeDAO;
private AttributeConverter fAttributeConverter;
public AttributeServiceImpl()
@@ -54,6 +57,11 @@ public class AttributeServiceImpl implements AttributeService
fGlobalAttributeEntryDAO = dao;
}
public void setAttributeDao(AttributeDAO dao)
{
fAttributeDAO = dao;
}
public void setAttributeConverter(AttributeConverter converter)
{
fAttributeConverter = converter;
@@ -138,9 +146,49 @@ public class AttributeServiceImpl implements AttributeService
/* (non-Javadoc)
* @see org.alfresco.service.cmr.attributes.AttributeService#query(java.lang.String, org.alfresco.service.cmr.attributes.AttrQuery)
*/
public List<Attribute> query(String path, AttrQuery query)
public List<Pair<String, Attribute>> query(String path, AttrQuery query)
{
return null;
if (path == null)
{
throw new AVMBadArgumentException("Null Attribute Path.");
}
List<String> keys = parsePath(path);
if (keys.size() == 0)
{
throw new AVMBadArgumentException("Cannot query top level Attributes.");
}
GlobalAttributeEntry entry = fGlobalAttributeEntryDAO.get(keys.get(0));
if (entry == null)
{
throw new AVMNotFoundException("Attribute Not Found: " + keys.get(0));
}
Attribute current = entry.getAttribute();
if (current.getType() != Type.MAP)
{
throw new AVMWrongTypeException("Attribute Not Map: " + keys.get(0));
}
for (int i = 1; i < keys.size(); i++)
{
current = current.get(keys.get(i));
if (current == null)
{
throw new AVMNotFoundException("Attribute Not Found: " + keys.get(i));
}
if (current.getType() != Type.MAP)
{
throw new AVMWrongTypeException("Attribute Not Map: " + keys.get(i));
}
}
List<Pair<String, Attribute>> rawResult =
fAttributeDAO.find((MapAttribute)current, query);
List<Pair<String, Attribute>> result =
new ArrayList<Pair<String, Attribute>>();
for (Pair<String, Attribute> raw : rawResult)
{
result.add(new Pair<String, Attribute>(raw.getFirst(),
fAttributeConverter.toValue(raw.getSecond())));
}
return result;
}
/* (non-Javadoc)
@@ -267,3 +315,4 @@ public class AttributeServiceImpl implements AttributeService
return new ArrayList<String>(current.keySet());
}
}

View File

@@ -27,7 +27,18 @@ package org.alfresco.repo.attributes;
import java.util.List;
import org.alfresco.service.cmr.attributes.AttrAndQuery;
import org.alfresco.service.cmr.attributes.AttrNotQuery;
import org.alfresco.service.cmr.attributes.AttrOrQuery;
import org.alfresco.service.cmr.attributes.AttrQueryEquals;
import org.alfresco.service.cmr.attributes.AttrQueryGT;
import org.alfresco.service.cmr.attributes.AttrQueryGTE;
import org.alfresco.service.cmr.attributes.AttrQueryLT;
import org.alfresco.service.cmr.attributes.AttrQueryLTE;
import org.alfresco.service.cmr.attributes.AttrQueryLike;
import org.alfresco.service.cmr.attributes.AttrQueryNE;
import org.alfresco.service.cmr.attributes.AttributeService;
import org.alfresco.util.Pair;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import junit.framework.TestCase;
@@ -38,9 +49,9 @@ import junit.framework.TestCase;
*/
public class AttributeServiceTest extends TestCase
{
private FileSystemXmlApplicationContext fContext = null;
private static FileSystemXmlApplicationContext fContext = null;
private AttributeService fService;
private static AttributeService fService;
/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
@@ -66,8 +77,6 @@ public class AttributeServiceTest extends TestCase
{
fService.removeAttribute("", key);
}
fContext.close();
fContext = null;
}
public void testBasic()
@@ -108,4 +117,114 @@ public class AttributeServiceTest extends TestCase
fail();
}
}
/**
* Test the query capability.
*/
public void testQuery()
{
try
{
// Put some attributes in place.
MapAttribute map = new MapAttributeValue();
map.put("a", new StringAttributeValue("a"));
map.put("b", new StringAttributeValue("a"));
map.put("c", new StringAttributeValue("a"));
map.put("d", new StringAttributeValue("a"));
map.put("e", new StringAttributeValue("a"));
map.put("f", new StringAttributeValue("a"));
map.put("g", new StringAttributeValue("a"));
map.put("h", new StringAttributeValue("a"));
map.put("i", new StringAttributeValue("a"));
map.put("j", new StringAttributeValue("a"));
map.put("k", new StringAttributeValue("a"));
map.put("l", new StringAttributeValue("a"));
map.put("m", new StringAttributeValue("a"));
map.put("n", new StringAttributeValue("a"));
map.put("o", new StringAttributeValue("a"));
map.put("p", new StringAttributeValue("a"));
map.put("q", new StringAttributeValue("a"));
map.put("r", new StringAttributeValue("a"));
map.put("s", new StringAttributeValue("a"));
map.put("t", new StringAttributeValue("a"));
map.put("u", new StringAttributeValue("a"));
map.put("v", new StringAttributeValue("a"));
map.put("w", new StringAttributeValue("a"));
map.put("x", new StringAttributeValue("a"));
map.put("y", new StringAttributeValue("a"));
map.put("z", new StringAttributeValue("a"));
fService.setAttribute("", "map1", map);
fService.setAttribute("", "map2", map);
List<Pair<String, Attribute>> result =
fService.query("map1", new AttrQueryEquals("w"));
assertEquals(1, result.size());
result =
fService.query("map1", new AttrQueryLT("d"));
assertEquals(3, result.size());
result =
fService.query("map1", new AttrQueryLTE("d"));
assertEquals(4, result.size());
result =
fService.query("map1", new AttrQueryGT("v"));
assertEquals(4, result.size());
result =
fService.query("map1", new AttrQueryGTE("v"));
assertEquals(5, result.size());
result =
fService.query("map1", new AttrQueryNE("g"));
assertEquals(25, result.size());
result =
fService.query("map1", new AttrNotQuery(new AttrQueryGT("d")));
assertEquals(4, result.size());
result =
fService.query("map1", new AttrAndQuery(new AttrQueryGT("g"),
new AttrQueryLT("l")));
assertEquals(4, result.size());
result =
fService.query("map1", new AttrOrQuery(new AttrQueryLT("d"),
new AttrQueryGT("w")));
assertEquals(6, result.size());
result =
fService.query("map1", new AttrQueryLike("%"));
assertEquals(26, result.size());
fService.setAttribute("map2", "submap", map);
result =
fService.query("map2/submap", new AttrQueryEquals("w"));
assertEquals(1, result.size());
result =
fService.query("map2/submap", new AttrQueryLT("d"));
assertEquals(3, result.size());
result =
fService.query("map2/submap", new AttrQueryLTE("d"));
assertEquals(4, result.size());
result =
fService.query("map2/submap", new AttrQueryGT("v"));
assertEquals(4, result.size());
result =
fService.query("map2/submap", new AttrQueryGTE("v"));
assertEquals(5, result.size());
result =
fService.query("map2/submap", new AttrQueryNE("g"));
assertEquals(25, result.size());
result =
fService.query("map2/submap", new AttrNotQuery(new AttrQueryGT("d")));
assertEquals(4, result.size());
result =
fService.query("map2/submap", new AttrAndQuery(new AttrQueryGT("g"),
new AttrQueryLT("l")));
assertEquals(4, result.size());
result =
fService.query("map2/submap", new AttrOrQuery(new AttrQueryLT("d"),
new AttrQueryGT("w")));
assertEquals(6, result.size());
result =
fService.query("map2/submap", new AttrQueryLike("%"));
assertEquals(26, result.size());
}
catch (Exception e)
{
e.printStackTrace();
fail();
}
}
}

View File

@@ -182,7 +182,7 @@ public class MapAttributeImpl extends AttributeImpl implements MapAttribute
public void put(String key, Attribute value)
{
MapEntry entry = AVMDAOs.Instance().fMapEntryDAO.get(this, key);
if (entry == null)
if (entry != null)
{
Attribute oldAttr = entry.getAttribute();
entry.setAttribute(value);

View File

@@ -25,15 +25,22 @@
package org.alfresco.repo.attributes.hibernate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.alfresco.repo.attributes.AttrQueryHelperImpl;
import org.alfresco.repo.attributes.Attribute;
import org.alfresco.repo.attributes.AttributeDAO;
import org.alfresco.repo.attributes.MapAttribute;
import org.alfresco.repo.attributes.MapEntry;
import org.alfresco.repo.attributes.MapEntryDAO;
import org.alfresco.repo.attributes.Attribute.Type;
import org.alfresco.service.cmr.attributes.AttrQuery;
import org.alfresco.service.cmr.attributes.AttrQueryHelper;
import org.alfresco.util.Pair;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
@@ -75,10 +82,25 @@ public class AttributeDAOHibernate extends HibernateDaoSupport implements
/* (non-Javadoc)
* @see org.alfresco.repo.attributes.AttributeDAO#find(java.lang.String, org.alfresco.service.cmr.attributes.AttrQuery)
*/
public List<Attribute> find(String path, AttrQuery query)
@SuppressWarnings("unchecked")
public List<Pair<String, Attribute>> find(MapAttribute map, AttrQuery query)
{
// TODO Need to implement query processing.
return null;
AttrQueryHelper helper = new AttrQueryHelperImpl();
String predicate = query.getPredicate(helper);
String fullQuery = "from MapEntryImpl me where me.map = :map and " + predicate;
Query hQuery = getSession().createQuery(fullQuery);
hQuery.setEntity("map", map);
for (Map.Entry<String, String> param : helper.getParameters().entrySet())
{
hQuery.setParameter(param.getKey(), param.getValue());
}
List<MapEntry> hits = (List<MapEntry>)hQuery.list();
List<Pair<String, Attribute>> result = new ArrayList<Pair<String, Attribute>>();
for (MapEntry entry : hits)
{
result.add(new Pair<String, Attribute>(entry.getKey(), entry.getAttribute()));
}
return result;
}
/* (non-Javadoc)

View File

@@ -44,8 +44,8 @@ public class AttrQueryEquals extends AttrQuery
@Override
public String getPredicate(AttrQueryHelper helper)
{
String name = ":name" + helper.getNextSuffix();
String name = "name" + helper.getNextSuffix();
helper.setParameter(name, fValue);
return "me.key = " + name;
return "me.key = :" + name;
}
}

View File

@@ -47,8 +47,8 @@ public class AttrQueryGT extends AttrQuery
@Override
public String getPredicate(AttrQueryHelper helper)
{
String name = ":name" + helper.getNextSuffix();
String name = "name" + helper.getNextSuffix();
helper.setParameter(name, fValue);
return "me.key > " + name;
return "me.key > :" + name;
}
}

View File

@@ -48,8 +48,8 @@ public class AttrQueryGTE extends AttrQuery
@Override
public String getPredicate(AttrQueryHelper helper)
{
String name = ":name" + helper.getNextSuffix();
String name = "name" + helper.getNextSuffix();
helper.setParameter(name, fValue);
return "me.key >= " + name;
return "me.key >= :" + name;
}
}

View File

@@ -47,8 +47,8 @@ public class AttrQueryLT extends AttrQuery
@Override
public String getPredicate(AttrQueryHelper helper)
{
String name = ":name" + helper.getNextSuffix();
String name = "name" + helper.getNextSuffix();
helper.setParameter(name, fValue);
return "me.key < " + name;
return "me.key < :" + name;
}
}

View File

@@ -47,8 +47,8 @@ public class AttrQueryLTE extends AttrQuery
@Override
public String getPredicate(AttrQueryHelper helper)
{
String name = ":name" + helper.getNextSuffix();
String name = "name" + helper.getNextSuffix();
helper.setParameter(name, fValue);
return "me.key <= " + name;
return "me.key <= :" + name;
}
}

View File

@@ -48,8 +48,8 @@ public class AttrQueryLike extends AttrQuery
@Override
public String getPredicate(AttrQueryHelper helper)
{
String name = ":name" + helper.getNextSuffix();
String name = "name" + helper.getNextSuffix();
helper.setParameter(name, fValue);
return "me.key like " + name;
return "me.key like :" + name;
}
}

View File

@@ -47,8 +47,8 @@ public class AttrQueryNE extends AttrQuery
@Override
public String getPredicate(AttrQueryHelper helper)
{
String name = ":name" + helper.getNextSuffix();
String name = "name" + helper.getNextSuffix();
helper.setParameter(name, fValue);
return "me.key <> " + name;
return "me.key <> :" + name;
}
}

View File

@@ -28,6 +28,7 @@ package org.alfresco.service.cmr.attributes;
import java.util.List;
import org.alfresco.repo.attributes.Attribute;
import org.alfresco.util.Pair;
/**
* This provides services for reading, writing, and querying global attributes.
@@ -62,7 +63,7 @@ public interface AttributeService
* @param query
* @return A List of matching attributes.
*/
public List<Attribute> query(String path, AttrQuery query);
public List<Pair<String, Attribute>> query(String path, AttrQuery query);
/**
* Get all the keys for a given attribute path.