Commits

Anonymous committed 9696de2

Comments (0)

Files changed (22)

encuestame-business/src/main/java/org/encuestame/business/service/SearchService.java

 package org.encuestame.business.service;
 
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.queryParser.ParseException;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.ScoreDoc;
+import org.apache.lucene.search.Searcher;
+import org.apache.lucene.search.TopScoreDocCollector;
+import org.apache.lucene.util.Version;
 import org.encuestame.business.service.imp.ISearchService;
+import org.encuestame.persistence.domain.survey.Poll;
 import org.encuestame.search.utils.Search;
 
 /**
 
     }
 
+    public void IndexPoll(Poll poll, String path) throws IOException{
+
+    }
+
+    /**************************************************************************/
+
+
+
+    public List<Poll> searchPolls(String searchString, String path, String fieldName,
+            int results) throws IOException, ParseException {
+        List<Poll> pollList = new ArrayList<Poll>();
+        IndexReader reader = IndexReader.open(path, true);
+        Searcher searcher = new IndexSearcher(reader);
+        QueryParser qp = new QueryParser(Version.LUCENE_29, fieldName,
+                new StandardAnalyzer(Version.LUCENE_29));
+        Query query = qp.parse(searchString);
+        ScoreDoc[] docs = searcher.search(query, results).scoreDocs;
+
+        for (int i = 0; i < docs.length; i++) {
+            Document doc = searcher.doc(docs[i].doc);
+            Poll articlePoll = new Poll();
+            articlePoll.setPollId(1L);
+            articlePoll.setName(doc.getField("TITLE").stringValue());
+            pollList.add(articlePoll);
+        }
+        return pollList;
+    }
+
+
+    public Map<String, Object> searchPollPaginateResults(String searchString,
+            String path, String fieldName, int page, int results)
+            throws IOException, ParseException {
+        Map<String, Object> map = new HashMap<String, Object>();
+        List<Poll> articles = new ArrayList<Poll>();
+        Searcher searcher = new IndexSearcher(IndexReader.open(path));
+        QueryParser qp = new QueryParser(Version.LUCENE_29, fieldName,
+                new StandardAnalyzer(Version.LUCENE_29));
+        Query query = qp.parse(searchString);
+        TopScoreDocCollector collector = TopScoreDocCollector.create(page * results, true);
+        searcher.search(query, collector);
+        ScoreDoc[] docs = collector.topDocs().scoreDocs;
+        map.put("resultados", collector.getTotalHits());
+        map.put("articulos", articles);
+        int startResult = (page - 1) * results;
+        if (startResult > docs.length) {
+            return map;
+        }
+        int end = Math.min(docs.length, startResult + results);
+
+        for (int i = startResult; i < end; i++) {
+            Document doc = searcher.doc(docs[i].doc);
+            Poll article = new Poll();
+            article.setPollId(1L);
+            article.setName(doc.getField("POLLNAME").stringValue());
+            articles.add(article);
+        }
+        return map;
+    }
+
+
+    public List<Poll> searchArticle(String searchString, String path, String fieldName,
+            int results) throws IOException, ParseException {
+        List<Poll> articles = new ArrayList<Poll>();
+        IndexReader reader = IndexReader.open(path);
+        Searcher searcher = new IndexSearcher(reader);
+        QueryParser qp = new QueryParser(Version.LUCENE_29, fieldName,
+                new StandardAnalyzer(Version.LUCENE_29));
+        Query query = qp.parse(searchString);
+        ScoreDoc[] docs = searcher.search(query, results).scoreDocs;
+
+        for (int i = 0; i < docs.length; i++) {
+            Document doc = searcher.doc(docs[i].doc);
+            Poll article = new Poll();
+            article.setPollId(1L);
+            article.setName((doc.getField("POLLNAME").stringValue()));
+            articles.add(article);
+        }
+        return articles;
+    }
+
+    public void searchArticleWithPagination(String path)
+        throws IOException, ParseException {
+        SearchService searcher = new SearchService();
+        Map<String, Object> map = searcher.searchPollPaginateResults("articulo", path, "POLLNAME", 1, 2);
+        List<Poll> articles = (List<Poll>) map.get("articulos");
+        System.out.println("Búsqueda finalizada, resultados: " + articles.size() + " de " + map.get("resultados"));
+        for (Poll articlePoll : articles) {
+        System.out.println("ID " + articlePoll.getPollId());
+        System.out.println("NAME POLL " + articlePoll.getName());
+        }
+    }
+
+
+    private Document generateDocumentFromPoll(Poll poll) {
+        Document doc = new Document();
+        doc.add(new Field("ID", String.valueOf(poll.getPollId()),
+                Field.Store.YES, Field.Index.NO));
+        doc.add(new Field("POLLNAME", poll.getName(),
+                Field.Store.YES, Field.Index.ANALYZED));
+        return doc;
+    }
+
+    public void indexPoll(Poll poll, String path) throws IOException {
+        IndexWriter writer = search.getIndexer(path);
+         try {
+            Document doc = generateDocumentFromPoll(poll);
+            writer.addDocument(doc);
+            writer.commit();
+            writer.optimize();
+        } finally {
+            writer.close();
+        }
+    }
+
 
 }

encuestame-business/src/main/java/org/encuestame/business/service/TweetPollService.java

     public Boolean validateUserTwitterAccount(final String username) throws EnMeDomainNotFoundException{
         final Account users = getUserAccount(username).getAccount();
         Boolean validate = false;
-        log.info(users.getTwitterAccount());
+     // TODO: Removed by ENCUESTAME-43
+    /*    log.info(users.getTwitterAccount());
         if(!users.getTwitterAccount().isEmpty() && !users.getTwitterPassword().isEmpty()){
             log.info(users.getTwitterPassword());
             try{
             catch (Exception e) {
                 log.error("Error Validate Twitter Account "+e.getMessage());
             }
-        }
+        }*/
         log.info(validate);
         return validate;
     }

encuestame-business/src/test/java/org/encuestame/test/business/service/TestSearchService.java

+package org.encuestame.test.business.service;
+
+import static org.junit.Assert.assertNotNull;
+
+import java.util.Date;
+
+import junit.framework.Assert;
+
+import org.encuestame.business.service.imp.IPollService;
+import org.encuestame.core.util.ConvertDomainBean;
+import org.encuestame.persistence.domain.Email;
+import org.encuestame.persistence.domain.EmailList;
+import org.encuestame.persistence.domain.Question;
+import org.encuestame.persistence.domain.security.Account;
+import org.encuestame.persistence.domain.security.UserAccount;
+import org.encuestame.persistence.domain.survey.Poll;
+import org.encuestame.persistence.domain.survey.PollFolder;
+import org.encuestame.persistence.domain.survey.QuestionPattern;
+import org.encuestame.test.business.service.config.AbstractServiceBase;
+import org.encuestame.utils.web.UnitPoll;
+import org.encuestame.utils.web.UnitQuestionBean;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+public class TestSearchService extends AbstractServiceBase {
+
+    /** {@link Account} **/
+    private Account user;
+
+    /** {@link Question} **/
+    private Question question;
+
+    /** {@link QuestionPattern} **/
+    private QuestionPattern questionPattern;
+
+    /** {@link IPollService} **/
+    @Autowired
+    private IPollService pollService;
+
+    /** {@link Poll} **/
+    private Poll poll;
+
+    private UserAccount userAccount;
+
+    /**
+     * Init.
+     */
+    @Before
+    public void serviceInit(){
+        this.user = createUser("testEncuesta", "testEncuesta123");
+        this.userAccount = createUserAccount("diana", this.user);
+        this.question = createQuestion("Why the roses are red?","html");
+        this.questionPattern = createQuestionPattern("html");
+        this.poll = createPoll(new Date(), this.question, "FDK125", this.user, Boolean.TRUE, Boolean.TRUE);
+      }
+
+    /**
+     * Test createPoll.
+     * @throws Exception exception
+     */
+    @Test
+    public void testcreatePoll() throws Exception{
+        final UnitQuestionBean question = ConvertDomainBean.convertQuestionsToBean(this.question);
+        final UnitPoll unitPoll = ConvertDomainBean.convertPollDomainToBean(this.poll);
+        unitPoll.setQuestionBean(question);
+        assertNotNull(unitPoll);
+        this.pollService.createPoll(unitPoll, this.userAccount.getUsername(), this.question);
+    }
+
+
+
+}

encuestame-business/src/test/java/org/encuestame/test/business/service/TestTweetPollService.java

         questionBean = createUnitQuestionBean(questionSave.getQuestion(), 1L, usersave.getUid(),
                    answersSaveTweet, patternBean);
         final UnitTweetPoll unitTweetPoll = createUnitTweetPollPublicated(new Date(), true, tweetUrl,usersave.getUid(),
-                                            this.questionBean, usersave.getTwitterAccount());
+                                            this.questionBean, null);
         unitTweetPoll.setId(tweetPoll.getTweetPollId());
         final String s = this.tweetPollService.generateTweetPollText(unitTweetPoll, tweetUrl);
         //final Status status = this.tweetPollService.publicTweetPoll(s, userpao.getTwitterAccount(), userpao.getTwitterPassword());

encuestame-business/src/test/java/org/encuestame/test/business/social/SocialServiceTestCase.java

 import org.encuestame.business.service.AbstractBaseService;
 import org.encuestame.business.service.imp.ILinkedInService;
 import org.encuestame.business.service.social.connect.TwitterSocialService;
+import org.junit.Ignore;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.Assert;
 
-
+@Ignore
 public class SocialServiceTestCase extends AbstractBaseService{
 
     /**
     /**
      * Test.
      */
-    @Test
-    public void twitterSocialServiceTest(){
+    @Ignore
+/*    public void twitterSocialServiceTest(){
         Assert.isTrue(true);
         System.out.println(this.linkedInService.getAuthorizeLinkedInUrl());
-    }
+    }*/
 
 
     /**

encuestame-core/src/main/java/org/encuestame/core/util/ConvertDomainBean.java

     public static final UnitSessionUserBean convertUserSessionToUserBean(final Account user){
         final UnitSessionUserBean sessionUserBean =  new UnitSessionUserBean();
         sessionUserBean.setUserSessionId(user.getUid());
-        sessionUserBean.setConsumerTwitterKey(user.getConsumerKey());
+     /*   sessionUserBean.setConsumerTwitterKey(user.getConsumerKey());
         sessionUserBean.setConsumerTwitterSecret(user.getConsumerSecret());
         sessionUserBean.setTwitterPassword(user.getTwitterPassword());
         sessionUserBean.setTwitterAccount(user.getTwitterAccount());
         sessionUserBean.setTwitterTwitterPing(user.getTwitterPing());
+     */
+        // TODO: Removed by ENCUESTAME-43
         return sessionUserBean;
     }
 

encuestame-persistence/src/main/java/org/encuestame/persistence/domain/security/Account.java

     /** User Id. **/
     private Long uid;
 
-    /** Twitter Account. **/
-    private String twitterAccount;
-
-    /** Twitter Password .**/
-    private String twitterPassword;
-
-    /** Consumer Key. **/
-    private String consumerKey;
-
-    /** Consumer Secret. **/
-    private String consumerSecret;
-
-    /** Twitter Pin. **/
-    private Integer twitterPing;
-
 /*
     private Set<CatLocationUser> catLocationUsers = new HashSet<CatLocationUser>(
             0);
         this.uid = uid;
     }
 
-    /**
-     * @return the twitterAccount
-     */
-    @Column(name = "twitter_account", nullable = true, length = 18)
-    public String getTwitterAccount() {
-        return twitterAccount;
-    }
-
-    /**
-     * @param twitterAccount the twitterAccount to set
-     */
-    public void setTwitterAccount(final String twitterAccount) {
-        this.twitterAccount = twitterAccount;
-    }
-
-    /**
-     * @return the twitterPassword
-     */
-    @Column(name = "twitter_password", nullable = true, length = 18)
-    public String getTwitterPassword() {
-        return twitterPassword;
-    }
-
-    /**
-     * @param twitterPassword the twitterPassword to set
-     */
-    public void setTwitterPassword(final String twitterPassword) {
-        this.twitterPassword = twitterPassword;
-    }
-
-    /**
-     * @return the consumerKey
-     */
-    @Column(name = "twitter_consumer_key", nullable = true)
-    public String getConsumerKey() {
-        return consumerKey;
-    }
-
-    /**
-     * @param consumerKey the consumerKey to set
-     */
-    public void setConsumerKey(String consumerKey) {
-        this.consumerKey = consumerKey;
-    }
-
-    /**
-     * @return the consumerSecret
-     */
-    @Column(name = "twitter_consumer_secret", nullable = true)
-    public String getConsumerSecret() {
-        return consumerSecret;
-    }
-
-    /**
-     * @param consumerSecret the consumerSecret to set
-     */
-    public void setConsumerSecret(String consumerSecret) {
-        this.consumerSecret = consumerSecret;
-    }
-
-    /**
-     * @return the twitterPing
-     */
-    @Column(name = "twitter_pin", nullable = true)
-    public Integer getTwitterPing() {
-        return twitterPing;
-    }
-
-    /**
-     * @param twitterPing the twitterPing to set
-     */
-    public void setTwitterPing(Integer twitterPing) {
-        this.twitterPing = twitterPing;
-    }
 }

encuestame-persistence/src/main/java/org/encuestame/persistence/util/EnMeSchemaExport.java

 
         //create user admin
         final Account userPrimary = new Account();
-        userPrimary.setTwitterAccount("testEncuesta");
-        userPrimary.setTwitterPassword("testEncuesta123");
+        //TODO: Delete Twitter properties. ENCUESTAME-43
+       // userPrimary.setTwitterAccount("testEncuesta");
+       // userPrimary.setTwitterPassword("testEncuesta123");
 
         final AccountDaoImp secUserDao = (AccountDaoImp) appContext.getBean("secUserDao");
         secUserDao.saveOrUpdate(userPrimary);

encuestame-persistence/src/test/java/org/encuestame/test/config/AbstractBase.java

      */
     public Account createUser(){
         Account user = new Account();
-        user.setTwitterAccount("testTWitterAccount");
-        user.setTwitterPassword("testTwitterPwsd");
+       // user.setTwitterAccount("testTWitterAccount");
+       // user.setTwitterPassword("testTwitterPwsd");
         getAccountDao().saveOrUpdate(user);
         return user;
     }
      */
     public Account createUser(final String twitterAccount, final String twitterPassword){
         Account user = new Account();
-        user.setTwitterAccount(twitterAccount);
-        user.setTwitterPassword(twitterPassword);
         getAccountDao().saveOrUpdate(user);
         return user;
     }

encuestame-persistence/src/test/java/org/encuestame/test/persistence/dao/TestPollDao.java

       **/
     @Test
     public void testFindAllPollByUserId(){
-        this.secUserSecondary = createUserAccount("diana", this.user);
+        this.secUserSecondary = createUserAccount("diana1", this.user);
         System.out.println("UID-->"+this.secUserSecondary.getUid());
         final List<Poll> pollList = getiPoll().findAllPollByUserId(this.user.getUid(),5,0);
         assertEquals("Should be equals", 1, pollList.size());

encuestame-persistence/src/test/java/org/encuestame/test/persistence/dao/TestSecGroupDaoImp.java

    public void TestDeleteGroup(){
         final Group group = super.createGroups("second group");
         getGroup().delete(group);
+        assertNotNull(this.secGroup);
         assertEquals("Should be equals",1, getGroup().findAllGroups().size());
     }
 

encuestame-persistence/src/test/java/org/encuestame/test/persistence/dao/TestUserDao.java

      */
     @Test
     public void testCreateUser() {
-       final UserAccount user = createUserAccount("user 1", this.account);
+       final UserAccount user = createUserAccount("user 3", this.account);
        assertNotNull(user);
     }
 
      */
     @Test
     public void testFindAllUsers() {
-        createUserAccount("user 1", this.account);
-        createUserAccount("user 2", this.account);
+        createUserAccount("user 4", this.account);
+        createUserAccount("user 5", this.account);
         assertEquals("Should be equals",3, getAccountDao().findAll().size());
     }
 
     public void testUpdateUser(){
         final String newPassword = "67809";
         final String newEmail = "user2@users.com";
-        final UserAccount user = createUserAccount("user 1", this.account);
+        final UserAccount user = createUserAccount("user 6", this.account);
         user.setPassword(newPassword);
         user.setUserEmail(newEmail);
         getAccountDao().saveOrUpdate(user);
          final UserAccount retrieveUser = getAccountDao()
-         .getUserAccountById(Long.valueOf(user.getUid()));
-     assertEquals("Password should be",newPassword,
-                     retrieveUser.getPassword());
-        assertEquals("Email should be",newEmail,
-               retrieveUser.getUserEmail());
+            .getUserAccountById(Long.valueOf(user.getUid()));
+        assertEquals("Password should be",newPassword, retrieveUser.getPassword());
+        assertEquals("Email should be",newEmail, retrieveUser.getUserEmail());
     }
 
     /**

encuestame-persistence/src/test/java/org/encuestame/test/persistence/domain/TestHibernateDomains.java

     @Test
     public void testSecUser(){
         final Account user = new Account();
-        user.setTwitterAccount("");
-        user.setTwitterPassword("");
-        getAccountDao().saveOrUpdate(user);
+         getAccountDao().saveOrUpdate(user);
         assertNotNull(user.getUid());
     }
 

encuestame-persistence/src/test/java/org/encuestame/test/persistence/domain/TestSecUsers.java

     @Test
     public void testSecUser(){
         final Account user = new Account();
-        user.setTwitterAccount("testTWitterAccount");
-        user.setTwitterPassword("testTwitterPwsd");
         getAccountDao().saveOrUpdate(user);
         assertNotNull(user.getUid());
     }

encuestame-search/src/main/java/org/encuestame/search/main/Indexer2.java

 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Iterator;
+import java.util.Set;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.lucene.analysis.StopAnalyzer;
         Document doc = new Document();
         if (StringUtils.isNotEmpty(docText)) {
         doc.add(new Field("content", docText,Field.Store.NO,Field.Index.ANALYZED));
+        doc.add(new Field("fullpath", file.getCanonicalPath(), Field.Store.YES,
+                Field.Index.NOT_ANALYZED)); // Index Full Path.
+        doc.add(new Field("filename", file.getName(), Field.Store.YES,
+                Field.Index.NOT_ANALYZED));
         }
         // extract PDF document's meta-data
 
        return doc;
        }
 
-    public void searcher(final String indexDir) throws CorruptIndexException, IOException, ParseException{
-        IndexReader reader = IndexReader.open(indexDir);
+    public void searcher(final String indexDir) throws CorruptIndexException,
+                        IOException, ParseException{
+        Directory dir = FSDirectory.open(new File(indexDir)); // Open Index
+        IndexReader reader = IndexReader.open(dir, true) ;
         IndexSearcher searcher = new IndexSearcher(reader);
         QueryParser parser = new QueryParser(Version.LUCENE_29, "content",
                 new StandardAnalyzer(Version.LUCENE_29));
         Query query = parser.parse("api");
-        TopDocCollector collector = new TopDocCollector(100);
+        TopScoreDocCollector  collector = TopScoreDocCollector.create(10, true);
         TopDocs hits2 = searcher.search(query, 3); // Search Index
 
         searcher.search(query , collector);
+
         ScoreDoc[] hits = collector.topDocs().scoreDocs;
         System.out.println("----------->" +hits2.totalHits );
-        String[] stopWords = StopAnalyzer.ENGLISH_STOP_WORDS;
+        Set stopWords = StopAnalyzer.ENGLISH_STOP_WORDS_SET;
         for(int i=0 ; i<hits.length ; i++) {
         int doc = hits[i].doc;
         Document doc1 = searcher.doc(doc);

encuestame-search/src/main/java/org/encuestame/search/main/PollIndexer.java

+/*
+ ************************************************************************************
+ * Copyright (C) 2001-2009 encuestame: system online surveys Copyright (C) 2009
+ * encuestame Development Team.
+ * Licensed under the Apache Software License version 2.0
+ * 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.
+ ************************************************************************************
+ */
+package org.encuestame.search.main;
+
+import java.io.IOException;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.index.IndexWriter;
+import org.encuestame.persistence.domain.survey.Poll;
+import org.encuestame.search.utils.Search;
+
+/**
+ * Poll Indexer.
+ * @author Morales, Diana Paola paola AT encuestame.org
+ * @since February 23, 2011
+ * @version $Id$
+ */
+public class PollIndexer {
+    private Search search;
+
+
+    private Document generateDocumentFromPoll(Poll poll) {
+        Document doc = new Document();
+        doc.add(new Field("ID", String.valueOf(poll.getPollId()),
+                Field.Store.YES, Field.Index.NO));
+        doc.add(new Field("POLLNAME", poll.getName(),
+                Field.Store.YES, Field.Index.ANALYZED));
+        return doc;
+    }
+
+    public void indexPoll(Poll poll, String path) throws IOException {
+        IndexWriter writer = search.getIndexer(path);
+         try {
+            Document doc = generateDocumentFromPoll(poll);
+            writer.addDocument(doc);
+            writer.commit();
+            writer.optimize();
+        } finally {
+            writer.close();
+        }
+    }
+
+
+}

encuestame-search/src/main/java/org/encuestame/search/utils/Search.java

      * @param indexDirPath Index Directory Path.
      * @throws IOException
      */
-    public void getIndexer(String indexDirPath) throws IOException{
+    public IndexWriter getIndexer(String indexDirPath) throws IOException{
         Directory dir = FSDirectory.open(new File(indexDirPath));
         this.writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_29) ,
                 true, IndexWriter.MaxFieldLength.UNLIMITED);
-
+        return writer;
     }
 
     /**
     public void setAnalyzer(Analyzer analyzer) {
         this.analyzer = analyzer;
     }
-
-
-
 }

encuestame-search/src/main/resources/Indexer2/_a.cfs

Binary file added.

encuestame-search/src/main/resources/Indexer2/segments.gen

Binary file modified.

encuestame-search/src/main/resources/Indexer2/segments_c

Binary file added.

encuestame-search/src/main/resources/encuestameStore/POI2.pdf

Binary file added.

encuestame-search/src/test/java/org/encuestame/search/TestSearch.java

  * @version $Id: $
  */
 public class TestSearch extends TestCase {
-    final String queryString = "indexed";
+    final String queryString = "indexing";
     final String indexDir = "src/main/resources/Indexer2";
 
     public void testSearcher() throws CorruptIndexException, IOException, ParseException{
             for (ScoreDoc scoreDoc : hits.scoreDocs) {
                 Document doc = searcher.doc(scoreDoc.doc); // Retrieving matching document.
                 System.out.println(doc);
+                System.out.println("*****************************************");
+                // get the filename
+                System.out.println(doc.get("filename"));
+                System.out.println(doc.getField("fullpath").stringValue());
             }
             searcher.close(); // Close IndexSearcher.
             System.out.println("Total number of docs "+reader.maxDoc());