[kune-commits] r1092 - in trunk/src: main/java/org/ourproject/kune/platf/server/content main/java/org/ourproject/kune/platf/server/domain main/java/org/ourproject/kune/platf/server/manager main/java/org/ourproject/kune/platf/server/manager/impl main/java/org/ourproject/kune/platf/server/rest test/java/org/ourproject/kune/platf/server/manager

vjrj vjrj at ourproject.org
Tue Apr 7 00:51:42 CEST 2009


Author: vjrj
Date: 2009-04-07 00:51:40 +0200 (Tue, 07 Apr 2009)
New Revision: 1092

Modified:
   trunk/src/main/java/org/ourproject/kune/platf/server/content/ContentManager.java
   trunk/src/main/java/org/ourproject/kune/platf/server/content/ContentManagerDefault.java
   trunk/src/main/java/org/ourproject/kune/platf/server/domain/BasicMimeType.java
   trunk/src/main/java/org/ourproject/kune/platf/server/domain/Content.java
   trunk/src/main/java/org/ourproject/kune/platf/server/manager/GroupManager.java
   trunk/src/main/java/org/ourproject/kune/platf/server/manager/Manager.java
   trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/DefaultManager.java
   trunk/src/main/java/org/ourproject/kune/platf/server/rest/ContentJSONService.java
   trunk/src/test/java/org/ourproject/kune/platf/server/manager/ContentManagerTest.java
Log:
Incomplete - task Media per group json search 


Modified: trunk/src/main/java/org/ourproject/kune/platf/server/content/ContentManager.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/platf/server/content/ContentManager.java	2009-04-06 10:00:04 UTC (rev 1091)
+++ trunk/src/main/java/org/ourproject/kune/platf/server/content/ContentManager.java	2009-04-06 22:51:40 UTC (rev 1092)
@@ -33,6 +33,21 @@
 
 public interface ContentManager extends Manager<Content, Long> {
 
+    String[] DEF_GLOBAL_SEARCH_FIELDS = new String[] { "authors.name", "authors.shortName", "container.name",
+            "language.code", "language.englishName", "language.nativeName", "lastRevision.body", "lastRevision.title" };
+
+    String[] DEF_GLOBAL_SEARCH_FIELDS_WITH_MIME = new String[] { "authors.name", "authors.shortName", "container.name",
+            "language.code", "language.englishName", "language.nativeName", "lastRevision.body", "lastRevision.title",
+            "mimeType.mimetype" };
+
+    String[] DEF_GROUP_SEARCH_FIELDS = new String[] { "container.owner.shortName", "authors.name", "authors.shortName",
+            "container.name", "language.code", "language.englishName", "language.nativeName", "lastRevision.body",
+            "lastRevision.title" };
+
+    String[] DEF_GROUP_SEARCH_FIELDS_WITH_MIME = new String[] { "authors.name", "authors.shortName", "container.name",
+            "language.code", "language.englishName", "language.nativeName", "lastRevision.body", "lastRevision.title",
+            "mimeType.mimetype" };
+
     void addAuthor(User user, Long contentId, String authorShortName) throws DefaultException;
 
     Content createContent(String title, String body, User author, Container container, String typeId);
@@ -57,6 +72,8 @@
 
     SearchResult<Content> search(String search, Integer firstResult, Integer maxResults);
 
+    SearchResult<Content> searchMime(String search, Integer firstResult, Integer maxResults, String mimetype);
+
     I18nLanguage setLanguage(User user, Long contentId, String languageCode) throws DefaultException;
 
     void setPublishedOn(User user, Long contentId, Date publishedOn) throws DefaultException;

Modified: trunk/src/main/java/org/ourproject/kune/platf/server/content/ContentManagerDefault.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/platf/server/content/ContentManagerDefault.java	2009-04-06 10:00:04 UTC (rev 1091)
+++ trunk/src/main/java/org/ourproject/kune/platf/server/content/ContentManagerDefault.java	2009-04-06 22:51:40 UTC (rev 1092)
@@ -27,6 +27,7 @@
 import org.apache.lucene.queryParser.MultiFieldQueryParser;
 import org.apache.lucene.queryParser.ParseException;
 import org.apache.lucene.search.Query;
+import org.apache.lucene.search.BooleanClause.Occur;
 import org.ourproject.kune.platf.client.errors.DefaultException;
 import org.ourproject.kune.platf.client.errors.I18nNotFoundException;
 import org.ourproject.kune.platf.client.errors.NameInUseException;
@@ -83,7 +84,7 @@
     }
 
     public Content createContent(final String title, final String body, final User author, final Container container,
-            String typeId) {
+            final String typeId) {
         FilenameUtils.checkBasicFilename(title);
         String newtitle = findInexistentTitle(container, title);
         final Content newContent = new Content();
@@ -99,7 +100,7 @@
         return persist(newContent);
     }
 
-    public boolean findIfExistsTitle(Container container, String title) {
+    public boolean findIfExistsTitle(final Container container, final String title) {
         return (contentFinder.findIfExistsTitle(container, title) > 0)
                 || (containerFinder.findIfExistsTitle(container, title) > 0);
     }
@@ -171,9 +172,7 @@
     }
 
     public SearchResult<Content> search(final String search, final Integer firstResult, final Integer maxResults) {
-        final MultiFieldQueryParser parser = new MultiFieldQueryParser(new String[] { "authors.name",
-                "authors.shortName", "container.name", "language.code", "language.englishName", "language.nativeName",
-                "lastRevision.body", "lastRevision.title", "tags.name" }, new StandardAnalyzer());
+        final MultiFieldQueryParser parser = createParser();
         Query query;
         try {
             query = parser.parse(search);
@@ -183,6 +182,12 @@
         return super.search(query, firstResult, maxResults);
     }
 
+    public SearchResult<Content> searchMime(final String search, final Integer firstResult, final Integer maxResults,
+            final String mimetype) {
+        return search(getSearchQueries(search, mimetype), DEF_GLOBAL_SEARCH_FIELDS_WITH_MIME, getConditions(), firstResult,
+                maxResults);
+    }
+
     public I18nLanguage setLanguage(final User user, final Long contentId, final String languageCode)
             throws DefaultException {
         final Content content = finder.getContent(contentId);
@@ -222,11 +227,34 @@
         tagManager.setTags(user, content, tags);
     }
 
-    private String findInexistentTitle(Container container, String title) {
+    private MultiFieldQueryParser createParser() {
+        final MultiFieldQueryParser parser = new MultiFieldQueryParser(DEF_GLOBAL_SEARCH_FIELDS, new StandardAnalyzer());
+        return parser;
+    }
+
+    private String findInexistentTitle(final Container container, final String title) {
         String initialTitle = new String(title);
         while (findIfExistsTitle(container, initialTitle)) {
             initialTitle = FileUtils.getNextSequentialFileName(initialTitle);
         }
         return initialTitle;
     }
+
+    private Occur[] getConditions() {
+        Occur[] conditions = new Occur[ContentManager.DEF_GLOBAL_SEARCH_FIELDS_WITH_MIME.length];
+        for (int i = 0; i < conditions.length; i++) {
+            conditions[i] = Occur.SHOULD;
+        }
+        conditions[conditions.length - 1] = Occur.MUST;
+        return conditions;
+    }
+
+    private String[] getSearchQueries(final String search, final String mimetype) {
+        String[] query = new String[ContentManager.DEF_GLOBAL_SEARCH_FIELDS_WITH_MIME.length];
+        for (int i = 0; i < query.length; i++) {
+            query[i] = search;
+        }
+        query[query.length - 1] = mimetype;
+        return query;
+    }
 }

Modified: trunk/src/main/java/org/ourproject/kune/platf/server/domain/BasicMimeType.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/platf/server/domain/BasicMimeType.java	2009-04-06 10:00:04 UTC (rev 1091)
+++ trunk/src/main/java/org/ourproject/kune/platf/server/domain/BasicMimeType.java	2009-04-06 22:51:40 UTC (rev 1092)
@@ -21,10 +21,16 @@
 
 import javax.persistence.Embeddable;
 
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Index;
+import org.hibernate.search.annotations.Store;
+
 @Embeddable
 public class BasicMimeType {
 
+    @Field(index = Index.TOKENIZED, store = Store.NO)
     private String mimetype;
+    @Field(index = Index.TOKENIZED, store = Store.NO)
     private String mimesubtype;
 
     public BasicMimeType() {

Modified: trunk/src/main/java/org/ourproject/kune/platf/server/domain/Content.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/platf/server/domain/Content.java	2009-04-06 10:00:04 UTC (rev 1091)
+++ trunk/src/main/java/org/ourproject/kune/platf/server/domain/Content.java	2009-04-06 22:51:40 UTC (rev 1092)
@@ -118,6 +118,7 @@
     @Column(nullable = false)
     private ContentStatus status;
 
+    @IndexedEmbedded
     @Embedded
     private BasicMimeType mimeType;
 
@@ -159,7 +160,7 @@
     }
 
     @Finder(query = "select count(*) from Container ctx, Content ctn where ctn.container.id = ctx.id and ctx = :container and ctn.lastRevision.title LIKE :title")
-    public Long findIfExistsTitle(@Named("container") Container container, @Named("title") String title) {
+    public Long findIfExistsTitle(@Named("container") final Container container, @Named("title") final String title) {
         return null;
     }
 

Modified: trunk/src/main/java/org/ourproject/kune/platf/server/manager/GroupManager.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/platf/server/manager/GroupManager.java	2009-04-06 10:00:04 UTC (rev 1091)
+++ trunk/src/main/java/org/ourproject/kune/platf/server/manager/GroupManager.java	2009-04-06 22:51:40 UTC (rev 1092)
@@ -61,8 +61,6 @@
 
     Group getSiteDefaultGroup();
 
-    void reIndex();
-
     SearchResult<Group> search(String search);
 
     SearchResult<Group> search(String search, Integer firstResult, Integer maxResults);

Modified: trunk/src/main/java/org/ourproject/kune/platf/server/manager/Manager.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/platf/server/manager/Manager.java	2009-04-06 10:00:04 UTC (rev 1091)
+++ trunk/src/main/java/org/ourproject/kune/platf/server/manager/Manager.java	2009-04-06 22:51:40 UTC (rev 1092)
@@ -19,6 +19,9 @@
  */
 package org.ourproject.kune.platf.server.manager;
 
+import org.apache.lucene.search.BooleanClause;
+import org.ourproject.kune.platf.server.manager.impl.SearchResult;
+
 public interface Manager<T, X> {
 
     T find(X id);
@@ -27,4 +30,15 @@
 
     T persist(T entity);
 
+    void reIndex();
+
+    SearchResult<T> search(final String query, final String[] fields, final BooleanClause.Occur[] flags,
+            final Integer firstResult, final Integer maxResults);
+
+    SearchResult<T> search(final String[] queries, final String[] fields, final BooleanClause.Occur[] flags,
+            final Integer firstResult, final Integer maxResults);
+
+    SearchResult<T> search(final String[] queries, final String[] fields, final Integer firstResult,
+            final Integer maxResults);
+
 }

Modified: trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/DefaultManager.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/DefaultManager.java	2009-04-06 10:00:04 UTC (rev 1091)
+++ trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/DefaultManager.java	2009-04-06 22:51:40 UTC (rev 1092)
@@ -23,6 +23,10 @@
 
 import javax.persistence.EntityManager;
 
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.queryParser.MultiFieldQueryParser;
+import org.apache.lucene.queryParser.ParseException;
+import org.apache.lucene.search.BooleanClause;
 import org.apache.lucene.search.Query;
 import org.hibernate.search.jpa.FullTextEntityManager;
 import org.hibernate.search.jpa.FullTextQuery;
@@ -90,6 +94,39 @@
         return new SearchResult<T>(new Long(emQuery.getResultSize()), emQuery.getResultList());
     }
 
+    public SearchResult<T> search(final String query, final String[] fields, final BooleanClause.Occur[] flags,
+            final Integer firstResult, final Integer maxResults) {
+        Query queryQ;
+        try {
+            queryQ = MultiFieldQueryParser.parse(query, fields, flags, new StandardAnalyzer());
+        } catch (final ParseException e) {
+            throw new RuntimeException("Error parsing search");
+        }
+        return search(queryQ, firstResult, maxResults);
+    }
+
+    public SearchResult<T> search(final String[] queries, final String[] fields, final BooleanClause.Occur[] flags,
+            final Integer firstResult, final Integer maxResults) {
+        Query query;
+        try {
+            query = MultiFieldQueryParser.parse(queries, fields, flags, new StandardAnalyzer());
+        } catch (final ParseException e) {
+            throw new RuntimeException("Error parsing search");
+        }
+        return search(query, firstResult, maxResults);
+    }
+
+    public SearchResult<T> search(final String[] queries, final String[] fields, final Integer firstResult,
+            final Integer maxResults) {
+        Query query;
+        try {
+            query = MultiFieldQueryParser.parse(queries, fields, new StandardAnalyzer());
+        } catch (final ParseException e) {
+            throw new RuntimeException("Error parsing search");
+        }
+        return search(query, firstResult, maxResults);
+    }
+
     /**
      * use carefully!!!
      */

Modified: trunk/src/main/java/org/ourproject/kune/platf/server/rest/ContentJSONService.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/platf/server/rest/ContentJSONService.java	2009-04-06 10:00:04 UTC (rev 1091)
+++ trunk/src/main/java/org/ourproject/kune/platf/server/rest/ContentJSONService.java	2009-04-06 22:51:40 UTC (rev 1092)
@@ -55,7 +55,17 @@
         SearchResult<Container> resultsContainer = containerManager.search(search, firstResult, maxResults);
         results.setSize(results.getSize() + resultsContainer.getSize());
         results.getList().addAll(results.getList());
+        return map(results);
+    }
+
+    @REST(params = { "query", "start", "limit", "mimetype" })
+    public SearchResultDTO<LinkDTO> search(final String search, final Integer firstResult, final Integer maxResults,
+            final String mimetype) {
+        SearchResult<Content> results = contentManager.searchMime(search, firstResult, maxResults, mimetype);
+        return map(results);
+    }
+
+    private SearchResultDTO<LinkDTO> map(final SearchResult<Content> results) {
         return mapper.mapSearchResult(results, LinkDTO.class);
     }
-
 }
\ No newline at end of file

Modified: trunk/src/test/java/org/ourproject/kune/platf/server/manager/ContentManagerTest.java
===================================================================
--- trunk/src/test/java/org/ourproject/kune/platf/server/manager/ContentManagerTest.java	2009-04-06 10:00:04 UTC (rev 1091)
+++ trunk/src/test/java/org/ourproject/kune/platf/server/manager/ContentManagerTest.java	2009-04-06 22:51:40 UTC (rev 1092)
@@ -7,21 +7,68 @@
 import org.ourproject.kune.platf.server.PersistencePreLoadedDataTest;
 import org.ourproject.kune.platf.server.domain.BasicMimeType;
 import org.ourproject.kune.platf.server.domain.Content;
+import org.ourproject.kune.platf.server.manager.impl.SearchResult;
 
 public class ContentManagerTest extends PersistencePreLoadedDataTest {
 
+    private static final String MIMETYPE = "image";
+    private static final String BODY = "body";
+    private static final String TITLE = "title";
+
     @Test
+    public void testBasicBodySearch() {
+        createContent();
+        SearchResult<Content> search = contentManager.search(BODY);
+        contentManager.reIndex();
+        assertEquals(new Long(1), search.getSize());
+    }
+
+    @Test
     public void testBasicMimePersist() {
         final String mimetype = "application/pdf";
-        createContentWithMime(mimetype);
+        createContentWithMimeAndCheck(mimetype);
     }
 
     @Test
     public void testBasicMimePersistWithoutSubtype() {
         final String mimetype = "application";
-        createContentWithMime(mimetype);
+        createContentWithMimeAndCheck(mimetype);
     }
 
+    @Test
+    public void testBasicMimeSearchWithQueriesAndFields() {
+        createContentWithMimeAndCheck(MIMETYPE + "/png");
+        contentManager.reIndex();
+        SearchResult<Content> search = contentManager.search(new String[] { MIMETYPE },
+                new String[] { "mimeType.mimetype" }, 0, 10);
+        assertEquals(new Long(1), search.getSize());
+    }
+
+    @Test
+    public void testBasicSearchWithQueriesAndFields() {
+        createContentWithMimeAndCheck(MIMETYPE);
+        SearchResult<Content> search = contentManager.search(new String[] { BODY },
+                new String[] { "lastRevision.body" }, 0, 10);
+        contentManager.reIndex();
+        assertEquals(new Long(1), search.getSize());
+    }
+
+    @Test
+    public void testBasicTitleSearch() {
+        createContent();
+        SearchResult<Content> search = contentManager.search(TITLE);
+        contentManager.reIndex();
+        assertEquals(new Long(1), search.getSize());
+    }
+
+    @Test
+    public void testMimeSearch() {
+        createContentWithMimeAndCheck(MIMETYPE + "/png");
+        contentManager.reIndex();
+        SearchResult<Content> search = contentManager.searchMime(BODY, 0, 10, MIMETYPE);
+        assertEquals(new Long(1), search.getSize());
+    }
+
     /**
      * This normally fails with mysql (not configured for utf-8), see the
      * INSTALL mysql section
@@ -34,9 +81,15 @@
         assertEquals("汉语/漢語", newCnt.getTitle());
     }
 
-    private void createContentWithMime(final String mimetype) {
-        final Content cnt = contentManager.createContent("title", "body", user, container,
+    private void createContent() {
+        final Content cnt = contentManager.createContent(TITLE, BODY, user, container,
                 DocumentServerTool.TYPE_UPLOADEDFILE);
+        persist(cnt);
+    }
+
+    private void createContentWithMimeAndCheck(final String mimetype) {
+        final Content cnt = contentManager.createContent(TITLE, BODY, user, container,
+                DocumentServerTool.TYPE_UPLOADEDFILE);
         cnt.setMimeType(new BasicMimeType(mimetype));
         persist(cnt);
         final Content newCnt = contentManager.find(cnt.getId());




More information about the kune-commits mailing list