DS-3004: Use the new name column on epersongroup when searching for groups and do not store the name in the metadata anymore.

This commit is contained in:
Tom Desair
2016-02-26 13:39:04 +01:00
parent 7ab4acf74c
commit 5798f58410
6 changed files with 84 additions and 92 deletions

View File

@@ -78,9 +78,6 @@ public class Group extends DSpaceObject implements DSpaceObjectLegacySupport
@Transient
private boolean groupsChanged;
@Transient
private transient GroupService groupService;
/**
* Protected constructor, create object using:
* {@link org.dspace.eperson.service.GroupService#create(Context)}
@@ -210,10 +207,6 @@ public class Group extends DSpaceObject implements DSpaceObjectLegacySupport
if(!StringUtils.equals(this.name, name)) {
this.name = name;
groupsChanged = true;
//Also update the name in the metadata
getGroupService().setMetadataSingleValue(context, this,
MetadataSchema.DC_SCHEMA, "title", null, null, name);
}
}
@@ -234,14 +227,6 @@ public class Group extends DSpaceObject implements DSpaceObjectLegacySupport
return supervisedItems;
}
private GroupService getGroupService() {
if(groupService == null)
{
groupService = EPersonServiceFactory.getInstance().getGroupService();
}
return groupService;
}
/**
* May this Group be renamed or deleted? (The content of any group may be
* changed.)

View File

@@ -28,6 +28,7 @@ import org.dspace.eperson.factory.EPersonServiceFactory;
import org.dspace.eperson.service.EPersonService;
import org.dspace.eperson.service.GroupService;
import org.dspace.event.Event;
import org.dspace.util.UUIDUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -293,37 +294,47 @@ public class GroupServiceImpl extends DSpaceObjectServiceImpl<Group> implements
}
@Override
public List<Group> search(Context context, String query) throws SQLException {
return search(context, query, -1, -1);
public List<Group> search(Context context, String groupIdentifier) throws SQLException {
return search(context, groupIdentifier, -1, -1);
}
@Override
public List<Group> search(Context context, String query, int offset, int limit) throws SQLException
public List<Group> search(Context context, String groupIdentifier, int offset, int limit) throws SQLException
{
try {
List<Group> groups = new ArrayList<>();
Group group = find(context, UUID.fromString(query));
List<Group> groups = new ArrayList<>();
UUID uuid = UUIDUtils.fromString(groupIdentifier);
if(uuid == null) {
//Search by group name
groups = groupDAO.findByNameLike(context, groupIdentifier, offset, limit);
} else {
//Search by group id
Group group = find(context, uuid);
if(group != null)
{
groups.add(group);
}
return groups;
} catch(IllegalArgumentException e) {
MetadataField nameField = metadataFieldService.findByElement(context, MetadataSchema.DC_SCHEMA, "title", null);
if(StringUtils.isBlank(query)) query = null;
return groupDAO.search(context, query, Arrays.asList(nameField), offset, limit);
}
return groups;
}
@Override
public int searchResultCount(Context context, String query) throws SQLException {
try {
return find(context, UUID.fromString(query)) != null ? 1 : 0;
} catch(IllegalArgumentException e) {
MetadataField nameField = metadataFieldService.findByElement(context, MetadataSchema.DC_SCHEMA, "title", null);
if (StringUtils.isBlank(query)) query = null;
return groupDAO.searchResultCount(context, query, Arrays.asList(nameField));
public int searchResultCount(Context context, String groupIdentifier) throws SQLException {
int result = 0;
UUID uuid = UUIDUtils.fromString(groupIdentifier);
if(uuid == null && StringUtils.isNotBlank(groupIdentifier)) {
//Search by group name
result = groupDAO.countByNameLike(context, groupIdentifier);
} else {
//Search by group id
Group group = find(context, uuid);
if(group != null)
{
result = 1;
}
}
return result;
}
@Override
@@ -648,7 +659,7 @@ public class GroupServiceImpl extends DSpaceObjectServiceImpl<Group> implements
}
else
{
return find(context, UUID.fromString(id));
return find(context, UUIDUtils.fromString(id));
}
}

View File

@@ -30,10 +30,6 @@ public interface GroupDAO extends DSpaceObjectDAO<Group>, DSpaceObjectLegacySupp
Group findByMetadataField(Context context, String searchValue, MetadataField metadataField) throws SQLException;
List<Group> search(Context context, String query, List<MetadataField> queryFields, int offset, int limit) throws SQLException;
int searchResultCount(Context context, String query, List<MetadataField> queryFields) throws SQLException;
List<Group> findAll(Context context, List<MetadataField> metadataFields, String sortColumn) throws SQLException;
List<Group> findByEPerson(Context context, EPerson ePerson) throws SQLException;
@@ -47,4 +43,8 @@ public interface GroupDAO extends DSpaceObjectDAO<Group>, DSpaceObjectLegacySupp
Group findByName(Context context, String name) throws SQLException;
Group findByNameAndEPerson(Context context, String groupName, EPerson ePerson) throws SQLException;
List<Group> findByNameLike(Context context, String groupIdentifier, int offset, int limit) throws SQLException;
int countByNameLike(Context context, String groupIdentifier) throws SQLException;
}

View File

@@ -7,9 +7,7 @@
*/
package org.dspace.eperson.dao.impl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.dspace.content.MetadataField;
import org.dspace.core.AbstractHibernateDSODAO;
@@ -20,7 +18,9 @@ import org.dspace.eperson.dao.GroupDAO;
import org.hibernate.Query;
import java.sql.SQLException;
import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
/**
* Hibernate implementation of the Database Access Object interface class for the Group object.
@@ -119,61 +119,30 @@ public class GroupDAOImpl extends AbstractHibernateDSODAO<Group> implements Grou
}
@Override
public List<Group> search(Context context, String query, List<MetadataField> queryFields, int offset, int limit) throws SQLException {
String groupTableName = "g";
String queryString = "SELECT " + groupTableName + " FROM Group as " + groupTableName;
Query hibernateQuery = getSearchQuery(context, queryString, query, queryFields, ListUtils.EMPTY_LIST, null);
public List<Group> findByNameLike(final Context context, final String groupName, final int offset, final int limit) throws SQLException {
Query query = createQuery(context,
"SELECT g FROM Group g WHERE lower(g.name) LIKE lower(:name)");
query.setParameter("name", "%" + StringUtils.trimToEmpty(groupName) + "%");
if(0 <= offset)
{
hibernateQuery.setFirstResult(offset);
query.setFirstResult(offset);
}
if(0 <= limit)
{
hibernateQuery.setMaxResults(limit);
query.setMaxResults(limit);
}
return list(hibernateQuery);
return list(query);
}
@Override
public int searchResultCount(Context context, String query, List<MetadataField> queryFields) throws SQLException {
String groupTableName = "g";
String queryString = "SELECT count(*) FROM Group as " + groupTableName;
Query hibernateQuery = getSearchQuery(context, queryString, query, queryFields, ListUtils.EMPTY_LIST, null);
public int countByNameLike(final Context context, final String groupName) throws SQLException {
Query query = createQuery(context,
"SELECT count(*) FROM Group g WHERE lower(g.name) LIKE lower(:name)");
query.setParameter("name", "%" + groupName + "%");
return count(hibernateQuery);
}
protected Query getSearchQuery(Context context, String queryString, String queryParam, List<MetadataField> queryFields, List<MetadataField> sortFields, String sortField) throws SQLException {
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append(queryString);
Set<MetadataField> metadataFieldsToJoin = new LinkedHashSet<>();
metadataFieldsToJoin.addAll(queryFields);
if(CollectionUtils.isNotEmpty(sortFields))
{
metadataFieldsToJoin.addAll(sortFields);
}
if(!CollectionUtils.isEmpty(metadataFieldsToJoin)) {
addMetadataLeftJoin(queryBuilder, "g", metadataFieldsToJoin);
}
if(queryParam != null) {
addMetadataValueWhereQuery(queryBuilder, queryFields, "like", null);
}
if(!CollectionUtils.isEmpty(sortFields)) {
addMetadataSortQuery(queryBuilder, sortFields, Collections.singletonList(sortField));
}
Query query = createQuery(context, queryBuilder.toString());
if(StringUtils.isNotBlank(queryParam)) {
query.setParameter("queryParam", "%"+queryParam+"%");
}
for (MetadataField metadataField : metadataFieldsToJoin) {
query.setParameter(metadataField.toString(), metadataField.getFieldID());
}
return query;
return count(query);
}
@Override

View File

@@ -168,20 +168,20 @@ public interface GroupService extends DSpaceObjectService<Group>, DSpaceObjectLe
*
* @param context
* DSpace context
* @param query
* The search string
* @param groupIdentifier
* The group name or group ID
*
* @return array of Group objects
*/
public List<Group> search(Context context, String query) throws SQLException;
public List<Group> search(Context context, String groupIdentifier) throws SQLException;
/**
* Find the groups that match the search query across eperson_group_id or name
*
* @param context
* DSpace context
* @param query
* The search string
* @param groupIdentifier
* The group name or group ID
* @param offset
* Inclusive offset
* @param limit
@@ -189,7 +189,7 @@ public interface GroupService extends DSpaceObjectService<Group>, DSpaceObjectLe
*
* @return array of Group objects
*/
public List<Group> search(Context context, String query, int offset, int limit) throws SQLException;
public List<Group> search(Context context, String groupIdentifier, int offset, int limit) throws SQLException;
/**
* Returns the total number of groups returned by a specific query, without the overhead

View File

@@ -0,0 +1,27 @@
package org.dspace.util;
import org.apache.commons.lang3.StringUtils;
import java.util.UUID;
/**
* Utility class to read UUIDs
*/
public class UUIDUtils {
public static UUID fromString(final String identifier) {
UUID output = null;
if(StringUtils.isNotBlank(identifier)) {
try {
output = UUID.fromString(identifier.trim());
} catch(IllegalArgumentException e) {
output = null;
}
}
return output;
}
public static String toString(final UUID identifier) {
return identifier == null ? null : identifier.toString();
}
}