This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository coselmar. See http://git.codelutin.com/coselmar.git commit fe40b40b0246dbd1d96678d25243b790443c985c Author: Yannick Martel <martel@©odelutin.com> Date: Thu Dec 10 12:26:14 2015 +0100 refs-30 #7778 Revue de la couche DAO pour la recherche de documents --- .../coselmar/persistence/SearchRequestBean.java | 14 +++- .../persistence/entity/DocumentTopiaDao.java | 94 ++++++++++++++++++++++ .../coselmar/services/v1/DocumentsWebService.java | 13 ++- 3 files changed, 116 insertions(+), 5 deletions(-) diff --git a/coselmar-persistence/src/main/java/fr/ifremer/coselmar/persistence/SearchRequestBean.java b/coselmar-persistence/src/main/java/fr/ifremer/coselmar/persistence/SearchRequestBean.java index 28d0243..927293a 100644 --- a/coselmar-persistence/src/main/java/fr/ifremer/coselmar/persistence/SearchRequestBean.java +++ b/coselmar-persistence/src/main/java/fr/ifremer/coselmar/persistence/SearchRequestBean.java @@ -34,6 +34,9 @@ public class SearchRequestBean implements Serializable { private static final long serialVersionUID = -5556404267735629642L; + public static final int DEFAULT_PAGE_SIZE = -1; + public static final int DEFAULT_PAGE = 0; + // Pagination Parameters protected int limit; protected int page; @@ -41,8 +44,11 @@ public class SearchRequestBean implements Serializable { // Global onFields request parameters protected List<String> fullTextSearch;// if this is given, make fullText search for all given string - public static long getSerialVersionUID() { - return serialVersionUID; + public static final SearchRequestBean newDefaultSearchRequestBean() { + SearchRequestBean searchRequestBean = new SearchRequestBean(); + searchRequestBean.setPage(DEFAULT_PAGE); + searchRequestBean.setLimit(DEFAULT_PAGE_SIZE); + return searchRequestBean; } public int getLimit() { @@ -51,7 +57,7 @@ public class SearchRequestBean implements Serializable { public void setLimit(Integer limit) { if (limit == null) { - this.limit = 10; + this.limit = DEFAULT_PAGE_SIZE; } else { this.limit = limit; } @@ -63,7 +69,7 @@ public class SearchRequestBean implements Serializable { public void setPage(Integer page) { if (page == null) { - this.page = 0; + this.page = DEFAULT_PAGE; } else { this.page = page; } diff --git a/coselmar-persistence/src/main/java/fr/ifremer/coselmar/persistence/entity/DocumentTopiaDao.java b/coselmar-persistence/src/main/java/fr/ifremer/coselmar/persistence/entity/DocumentTopiaDao.java index 4a0bb09..c5b691d 100644 --- a/coselmar-persistence/src/main/java/fr/ifremer/coselmar/persistence/entity/DocumentTopiaDao.java +++ b/coselmar-persistence/src/main/java/fr/ifremer/coselmar/persistence/entity/DocumentTopiaDao.java @@ -29,6 +29,8 @@ import java.util.List; import java.util.Map; import fr.ifremer.coselmar.persistence.DaoUtils; +import fr.ifremer.coselmar.persistence.SearchRequestBean; +import org.apache.commons.lang3.StringUtils; public class DocumentTopiaDao extends AbstractDocumentTopiaDao<Document> { @@ -100,6 +102,98 @@ public class DocumentTopiaDao extends AbstractDocumentTopiaDao<Document> { return documents; } + public List<Document> findAllByExample(CoselmarUser userFilter, Document example, SearchRequestBean requestBean) { + + StringBuilder hqlBuilder = new StringBuilder("SELECT DISTINCT(D) FROM " + Document.class.getName() + " D" + + " LEFT OUTER JOIN D." + Document.PROPERTY_RESTRICTED_LIST + " CUG "); + hqlBuilder.append(" WHERE 1=1 "); // Just because next clause will begin with operator + + Map<String, Object> args = new HashMap<>(); + + // If there is a filter on User : list public, owned and ones he is on restricted list. + if (userFilter != null) { + // can list all public document + String privacyPublicCondition = DaoUtils.getQueryForAttributeEquals("D", Document.PROPERTY_PRIVACY, args, Privacy.PUBLIC, ""); + hqlBuilder.append(" AND ( " + privacyPublicCondition); + + // Can list his own document + String ownerCondition = DaoUtils.orAttributeEquals("D", Document.PROPERTY_OWNER, args, userFilter); + hqlBuilder.append(ownerCondition); + + // For limited access, check if user is in a restricted list + String participantCondition = DaoUtils.orAttributeContains("CUG", CoselmarUserGroup.PROPERTY_MEMBERS, args, userFilter); + hqlBuilder.append(participantCondition + ")"); + } + + + // Manage example + if (example != null) { + hqlBuilder.append(" AND ( 1 = 1 "); + + if (StringUtils.isNotBlank(example.getName())) { + String nameCondition = DaoUtils.andAttributeLike("D", Document.PROPERTY_NAME, args, example.getName()); + hqlBuilder.append(nameCondition); + } + + if (StringUtils.isNotBlank(example.getAuthors())) { + String authorsCondition = DaoUtils.andAttributeLike("D", Document.PROPERTY_AUTHORS, args, example.getAuthors()); + hqlBuilder.append(authorsCondition); + } + + if (StringUtils.isNotBlank(example.getLicense())) { + String licenseCondition = DaoUtils.andAttributeLike("D", Document.PROPERTY_LICENSE, args, example.getLicense()); + hqlBuilder.append(licenseCondition); + } + + if (StringUtils.isNotBlank(example.getType())) { + String typeCondition = DaoUtils.andAttributeLike("D", Document.PROPERTY_TYPE, args, example.getType()); + hqlBuilder.append(typeCondition); + } + + if (example.getPrivacy() != null ) { + String privacyCondition = DaoUtils.andAttributeEquals("D", Document.PROPERTY_PRIVACY, args, example.getPrivacy()); + hqlBuilder.append(privacyCondition); + } + + if (example.getKeywords() != null && !example.getKeywords().isEmpty()) { + for (String keyword : example.getKeywords()) { + String keywordCondition = DaoUtils.andAttributeContains("D", Document.PROPERTY_KEYWORDS, args, keyword); + hqlBuilder.append(keywordCondition); + } + } + + if (example.getOwner() != null) { + String ownerCondition = DaoUtils.andAttributeEquals("D", Document.PROPERTY_OWNER, args, example.getOwner()); + hqlBuilder.append(ownerCondition); + } + + hqlBuilder.append(" ) "); + } + + + // Manage keywords search in : title, summary, authors and keywords + if (requestBean.getFullTextSearch() != null && !requestBean.getFullTextSearch().isEmpty()) { + hqlBuilder.append(" AND ( 1 = 0 "); + for (String keyword : requestBean.getFullTextSearch()) { + String nameClause = DaoUtils.orAttributeLike("D", Document.PROPERTY_NAME, args, keyword); + String summaryClause = DaoUtils.orAttributeLike("D", Document.PROPERTY_SUMMARY, args, keyword); + String authorsClause = DaoUtils.orAttributeLike("D", Document.PROPERTY_AUTHORS, args, keyword); + String containsKeyword = DaoUtils.orAttributeContains("D", Document.PROPERTY_KEYWORDS, args, keyword); + + hqlBuilder.append(nameClause); + hqlBuilder.append(summaryClause); + hqlBuilder.append(authorsClause); + hqlBuilder.append(containsKeyword); + } + hqlBuilder.append(" )"); + } + + + List<Document> documents = forHql(hqlBuilder.toString(), args).findAll(); + + return documents; + } + public List<String> findAllKeywords() { StringBuilder hqlBuilder = diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/DocumentsWebService.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/DocumentsWebService.java index 482ae7c..6dd657f 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/DocumentsWebService.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/DocumentsWebService.java @@ -154,6 +154,17 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { CoselmarUserRole currentUserRole = currentUser.getRole(); + SearchRequestBean requestBean = SearchRequestBean.newDefaultSearchRequestBean(); + if (searchBean != null) { + if (searchBean.getPage() != null) { + requestBean.setPage(searchBean.getPage()); + } + if (searchBean.getLimit() != null) { + requestBean.setLimit(searchBean.getLimit()); + } + requestBean.setFullTextSearch(searchBean.getFullTextSearch()); + } + List<Document> documentList; // Admin and Supervisor can see all documents (public, private and restricted) @@ -181,7 +192,7 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { } else { //Other can only see public, his own private and restricted for which he is allowed - documentList = getDocumentDao().findAllFilterByUser(currentUser, searchBean.getFullTextSearch()); + documentList = getDocumentDao().findAllByExample(currentUser, null, requestBean); } List<DocumentBean> result = new ArrayList<>(documentList.size()); -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.