]> SALOME platform Git repositories - tools/siman.git/blobdiff - Workspace/Siman-Common/src/org/splat/service/technical/IndexServiceImpl.java
Salome HOME
Modifications done to respect PMD rules. Versioning a document is fixed. Validation...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / technical / IndexServiceImpl.java
index 9ed81a75a46db8cffdab958ccba44d6457194b8a..7e7602086572b28f2c68b4d5adfd9a28f9bc692f 100644 (file)
@@ -14,7 +14,6 @@ import java.util.List;
 import java.util.Set;
 
 import org.apache.log4j.Logger;
-
 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 import org.apache.lucene.document.Field;
 import org.apache.lucene.index.CorruptIndexException;
@@ -40,93 +39,102 @@ import org.splat.som.Step;
  */
 public class IndexServiceImpl implements IndexService {
 
-       private Directory index;
-       private org.apache.lucene.document.Document body;
+       /**
+        * The logger for this service.
+        */
+       private static final Logger LOG = Logger
+       .getLogger(IndexServiceImpl.class);
+
+       private final static String PROP_INDEX  = "index";
+       private final static String PROP_REF    = "ref";
+       private final static String PROP_STATE  = "state";
+       private final static String PROP_AUTHOR = "author";
+       private final static String PROP_TITLE  = "title";
+       
+       private transient Directory _index;
+       private transient org.apache.lucene.document.Document _body;
        private ProjectElementService _projectElementService;
        private RepositoryService _repositoryService;
 
        protected static StandardAnalyzer analyzer = new StandardAnalyzer(
                        Version.LUCENE_29);
-       private static final Logger logger = Logger
-                       .getLogger(IndexServiceImpl.class);
-
        private class Entry extends IndexWriter {
-               private org.apache.lucene.document.Document entry;
+               private transient final org.apache.lucene.document.Document _entry;
 
-               private Entry(Study study) throws CorruptIndexException,
+               private Entry(final Study study) throws CorruptIndexException,
                                LockObtainFailedException, IOException {
-                       super(index, analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
+                       super(_index, analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
 
                        // Addition of mandatory fields
-                       entry = new org.apache.lucene.document.Document();
+                       _entry = new org.apache.lucene.document.Document();
                        Field field;
-                       field = body.getField("index");
+                       field = _body.getField(PROP_INDEX);
                        field.setValue(String.valueOf(study.getIndex()));
-                       entry.add(field);
-                       field = body.getField("class");
+                       _entry.add(field);
+                       field = _body.getField("class");
                        field.setValue("Study");
-                       entry.add(field);
-                       field = body.getField("type");
+                       _entry.add(field);
+                       field = _body.getField("type");
                        field.setValue(""); // Reserved for configurable Study type
-                       entry.add(field);
-                       field = body.getField("ref");
+                       _entry.add(field);
+                       field = _body.getField(PROP_REF);
                        field.setValue(study.getReference());
-                       entry.add(field);
-                       field = body.getField("area");
+                       _entry.add(field);
+                       field = _body.getField("area");
                        field.setValue(study.getVisibility().toString());
-                       entry.add(field);
-                       field = body.getField("state");
+                       _entry.add(field);
+                       field = _body.getField(PROP_STATE);
                        field.setValue(study.getProgressState().toString());
-                       entry.add(field);
-                       field = body.getField("author");
+                       _entry.add(field);
+                       field = _body.getField(PROP_AUTHOR);
                        field.setValue(study.getAuthor().toString());
-                       entry.add(field);
-                       field = body.getField("title");
+                       _entry.add(field);
+                       field = _body.getField(PROP_TITLE);
                        field.setValue(study.getTitle());
-                       entry.add(field);
-                       field = body.getField("contents");
+                       _entry.add(field);
+                       field = _body.getField("contents");
                        field.setValue(study.getTitle());
-                       entry.add(field);
+                       _entry.add(field);
 
                        // Addition of optional fields
                        setActorsOf(study);
                        setContextAt(getProjectElementService().getSteps(study));
                }
 
-               private Entry(KnowledgeElement kelm) throws CorruptIndexException,
+               private Entry(final KnowledgeElement kelm) throws CorruptIndexException,
                                LockObtainFailedException, IOException {
-                       super(index, analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
+                       super(_index, analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
 
                        // Addition of mandatory fields
-                       entry = new org.apache.lucene.document.Document();
+                       _entry = new org.apache.lucene.document.Document();
                        Field field;
-                       field = body.getField("index");
+                       field = _body.getField(PROP_INDEX);
                        field.setValue(String.valueOf(kelm.getIndex()));
-                       entry.add(field);
-                       field = body.getField("class");
+                       _entry.add(field);
+                       field = _body.getField("class");
                        field.setValue("KnowledgeElement");
-                       entry.add(field);
-                       field = body.getField("type");
+                       _entry.add(field);
+                       field = _body.getField("type");
                        field.setValue(kelm.getType().getName());
-                       entry.add(field);
-                       field = body.getField("ref");
+                       _entry.add(field);
+                       field = _body.getField(PROP_REF);
                        field.setValue(kelm.getReference());
-                       entry.add(field);
-                       field = body.getField("area");
+                       _entry.add(field);
+                       field = _body.getField("area");
                        field.setValue(kelm.getVisibility().toString());
-                       entry.add(field);
-                       field = body.getField("state");
+                       _entry.add(field);
+                       field = _body.getField(PROP_STATE);
                        field.setValue(kelm.getProgressState().toString());
-                       entry.add(field);
-                       field = body.getField("author");
+                       _entry.add(field);
+                       field = _body.getField(PROP_AUTHOR);
                        field.setValue(kelm.getAuthor().toString());
-                       entry.add(field);
-                       field = body.getField("title");
+                       _entry.add(field);
+                       field = _body.getField(PROP_TITLE);
                        field.setValue(kelm.getTitle());
-                       entry.add(field);
-                       field = body.getField("contents");
+                       _entry.add(field);
+                       field = _body.getField("contents");
                        field.setValue(kelm.getTitle());
-                       entry.add(field);
+                       _entry.add(field);
 
                        // TODO: Addition of optional fields
                        Scenario scene = kelm.getOwnerScenario();
@@ -137,22 +145,22 @@ public class IndexServiceImpl implements IndexService {
                }
 
                private void add() throws CorruptIndexException, IOException {
-                       addDocument(entry);
+                       addDocument(_entry);
                        // Save the new entry
                        optimize(); // Should be called before committing the index
                        close(); // Commits the index
                }
 
                private void update() throws CorruptIndexException, IOException {
-                       String value = entry.getField("ref").stringValue(); // Only field with unique value
-                       Term term = new Term("ref").createTerm(value);
-                       updateDocument(term, entry);
+                       String value = _entry.getField(PROP_REF).stringValue(); // Only field with unique value
+                       Term term = new Term(PROP_REF).createTerm(value);
+                       updateDocument(term, _entry);
                        // Save the updated entry
                        optimize(); // Should be called before committing the index
                        close(); // Commits the index
                }
 
-               private void setContextAt(Step[] step) {
+               private void setContextAt(final Step[] step) {
                        for (int i = 0; i < step.length; i++) {
                                List<SimulationContext> contexts = step[i]
                                                .getAllSimulationContexts();
@@ -161,72 +169,71 @@ public class IndexServiceImpl implements IndexService {
                                        SimulationContext context = j.next();
                                        String type = String.valueOf(context.getType().getIndex());
                                        String value = context.getValue();
-                                       entry.add(new Field(type, value, Field.Store.NO,
+                                       _entry.add(new Field(type, value, Field.Store.NO,
                                                        Field.Index.NOT_ANALYZED));
                                }
                        }
                }
 
-               private void setActorsOf(Study study) {
+               private void setActorsOf(final Study study) {
                        // RKV: This set is always not null. Let's assume that it must be initialized before reindexing: Set<User> actors =
                        // study.getActors();
                        Set<User> actors = study.getActor(); // RKV
-                       if (logger.isDebugEnabled()) {
-                               logger.debug("Study " + study.getReference()
+                       if (LOG.isDebugEnabled()) {
+                               LOG.debug("Study " + study.getReference()
                                                + " actors number to be added to the lucen index: "
                                                + actors.size());
                        }
                        for (Iterator<User> i = actors.iterator(); i.hasNext();) {
                                String value = i.next().toString();
-                               entry.add(new Field("actor", value, Field.Store.NO,
+                               _entry.add(new Field("actor", value, Field.Store.NO,
                                                Field.Index.NOT_ANALYZED));
-                               if (logger.isDebugEnabled()) {
-                                       logger.debug("    actor added to the lucen index: " + value);
+                               if (LOG.isDebugEnabled()) {
+                                       LOG.debug("    actor added to the lucen index: " + value);
                                }
                        }
                }
        }
 
        public static class ObjectProxy implements Proxy, Serializable {
-               // --------------------------------------------------------------
-               private Long rid;
-               private String sid;
-               private ProgressState state;
-               private String title;
-               private String type;
-               private String name;
+               private transient final Long _rid;
+               private transient final String _sid;
+               private transient final ProgressState _state;
+               private transient final String _title;
+               private transient String _type;
+               private transient final String _name;
                private static final long serialVersionUID = -4386494192709562221L;
 
-               public ObjectProxy(org.apache.lucene.document.Document ludoc) {
-                       rid = Long.valueOf(ludoc.get("index"));
-                       sid = ludoc.get("ref");
-                       state = ProgressState.valueOf(ludoc.get("state"));
-                       title = ludoc.get("title");
-                       name = ludoc.get("author");
+               public ObjectProxy(final org.apache.lucene.document.Document ludoc) {
+                       _rid = Long.valueOf(ludoc.get(PROP_INDEX));
+                       _sid = ludoc.get(PROP_REF);
+                       _state = ProgressState.valueOf(ludoc.get(PROP_STATE));
+                       _title = ludoc.get(PROP_TITLE);
+                       _name = ludoc.get(PROP_AUTHOR);
                }
 
                public String getAuthorName() {
-                       return name;
+                       return _name;
                }
 
                public Long getIndex() {
-                       return rid;
+                       return _rid;
                }
 
                public ProgressState getProgressState() {
-                       return state;
+                       return _state;
                }
 
                public String getReference() {
-                       return sid;
+                       return _sid;
                }
 
                public String getTitle() {
-                       return title;
+                       return _title;
                }
 
                public String getType() {
-                       return type;
+                       return _type;
                }
        }
 
@@ -236,35 +243,35 @@ public class IndexServiceImpl implements IndexService {
 
        public void configure() throws IOException {
                File indir = getRepositoryService().getRepositoryIndexDirectory();
-               index = FSDirectory.open(indir);
-               body = new org.apache.lucene.document.Document();
-               body.add(new Field("index", "", Field.Store.YES,
+               _index = FSDirectory.open(indir);
+               _body = new org.apache.lucene.document.Document();
+               _body.add(new Field(PROP_INDEX, "", Field.Store.YES,
                                Field.Index.NOT_ANALYZED));
-               body.add(new Field("class", "", Field.Store.NO,
+               _body.add(new Field("class", "", Field.Store.NO,
                                Field.Index.NOT_ANALYZED));
-               body.add(new Field("type", "", Field.Store.YES,
+               _body.add(new Field("type", "", Field.Store.YES,
                                Field.Index.NOT_ANALYZED));
-               body
-                               .add(new Field("ref", "", Field.Store.YES,
+               _body
+                               .add(new Field(PROP_REF, "", Field.Store.YES,
                                                Field.Index.NOT_ANALYZED));
-               body
+               _body
                                .add(new Field("area", "", Field.Store.NO,
                                                Field.Index.NOT_ANALYZED));
-               body.add(new Field("state", "", Field.Store.YES,
+               _body.add(new Field(PROP_STATE, "", Field.Store.YES,
                                Field.Index.NOT_ANALYZED));
-               body.add(new Field("author", "", Field.Store.YES,
+               _body.add(new Field(PROP_AUTHOR, "", Field.Store.YES,
                                Field.Index.NOT_ANALYZED));
-               body.add(new Field("title", "", Field.Store.YES,
+               _body.add(new Field(PROP_TITLE, "", Field.Store.YES,
                                Field.Index.NOT_ANALYZED));
-               body
+               _body
                                .add(new Field("contents", "", Field.Store.NO,
                                                Field.Index.ANALYZED));
-               if (!this.exists())
+               if (!this.exists()) {
                        this.create(); // Happens when re-indexing all studies
+               }
        }
 
        public void create() throws IOException {
-               // -------------------------------
                Directory index = FSDirectory.open(getRepositoryService()
                                .getRepositoryIndexDirectory());
                IndexWriter writer = new IndexWriter(index, analyzer, true,
@@ -276,49 +283,45 @@ public class IndexServiceImpl implements IndexService {
        // Member functions
        // ==============================================================================================================================
 
-       public void add(Study study) throws IOException {
-               // --------------------------------
+       public void add(final Study study) throws IOException {
                IndexServiceImpl.Entry entry = new Entry(study);
                entry.add();
-               if (logger.isInfoEnabled()) {
-                       logger.info("Study \"" + study.getIndex() + "\" indexed.");
+               if (LOG.isInfoEnabled()) {
+                       LOG.info("Study \"" + study.getIndex() + "\" indexed.");
                }
        }
 
-       public void add(KnowledgeElement kelm) throws IOException {
-               // ------------------------------------------
+       public void add(final KnowledgeElement kelm) throws IOException {
                IndexServiceImpl.Entry entry = new Entry(kelm);
                entry.add();
-               if (logger.isInfoEnabled()) {
-                       logger.info("Knowledge \"" + kelm.getIndex() + "\" indexed.");
+               if (LOG.isInfoEnabled()) {
+                       LOG.info("Knowledge \"" + kelm.getIndex() + "\" indexed.");
                }
        }
 
        public boolean exists() {
-               // ---------------------------
+               boolean res = false;
                try {
-                       return IndexReader.indexExists(index);
+                       res = IndexReader.indexExists(_index);
                } catch (IOException error) {
-                       error.printStackTrace();
-                       return false;
+                       LOG.error(error.getMessage(), error);
                }
+               return res;
        }
 
-       public void update(Study study) throws IOException {
-               // -----------------------------------
+       public void update(final Study study) throws IOException {
                IndexServiceImpl.Entry entry = new Entry(study);
                entry.update();
-               if (logger.isInfoEnabled()) {
-                       logger.info("Study \"" + study.getIndex() + "\" re-indexed.");
+               if (LOG.isInfoEnabled()) {
+                       LOG.info("Study \"" + study.getIndex() + "\" re-indexed.");
                }
        }
 
-       public void update(KnowledgeElement kelm) throws IOException {
-               // ---------------------------------------------
+       public void update(final KnowledgeElement kelm) throws IOException {
                IndexServiceImpl.Entry entry = new Entry(kelm);
                entry.update();
-               if (logger.isInfoEnabled()) {
-                       logger.info("Knowledge \"" + kelm.getIndex() + "\" re-indexed.");
+               if (LOG.isInfoEnabled()) {
+                       LOG.info("Knowledge \"" + kelm.getIndex() + "\" re-indexed.");
                }
        }
 
@@ -338,7 +341,7 @@ public class IndexServiceImpl implements IndexService {
         *            the projectElementService to set
         */
        public void setProjectElementService(
-                       ProjectElementService projectElementService) {
+                       final ProjectElementService projectElementService) {
                _projectElementService = projectElementService;
        }
 
@@ -357,7 +360,7 @@ public class IndexServiceImpl implements IndexService {
         * @param repositoryService
         *            the repositoryService to set
         */
-       public void setRepositoryService(RepositoryService repositoryService) {
+       public void setRepositoryService(final RepositoryService repositoryService) {
                _repositoryService = repositoryService;
        }
 }
\ No newline at end of file