Salome HOME
Refactoring: configuration files are moved into Siman web project.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / som / Index.java
1 package org.splat.som;
2 /**
3  * 
4  * @author    Daniel Brunier-Coulin
5  * @copyright OPEN CASCADE 2012
6  */
7
8 import java.io.File;
9 import java.io.IOException;
10 import java.io.Serializable;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Set;
14
15 import org.apache.log4j.Logger;
16
17 import org.apache.lucene.analysis.standard.StandardAnalyzer;
18 import org.apache.lucene.document.Field;
19 import org.apache.lucene.index.CorruptIndexException;
20 import org.apache.lucene.index.IndexReader;
21 import org.apache.lucene.index.IndexWriter;
22 import org.apache.lucene.index.Term;
23 import org.apache.lucene.store.Directory;
24 import org.apache.lucene.store.FSDirectory;
25 import org.apache.lucene.store.LockObtainFailedException;
26 import org.apache.lucene.util.Version;
27 import org.splat.kernel.User;
28
29
30 class Index {
31
32     private Directory                            index;
33     private org.apache.lucene.document.Document  body;
34     
35         protected static       StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_29);
36     private   static final Logger           logger   = Logger.getLogger(Index.class);
37
38     private class Entry extends IndexWriter {
39 //  ---------------------------------------
40       private org.apache.lucene.document.Document entry;
41
42       private Entry (Study study) throws CorruptIndexException, LockObtainFailedException, IOException
43       {
44         super(index, analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
45
46 //      Addition of mandatory fields
47         entry = new org.apache.lucene.document.Document();        
48         Field  field;
49         field = body.getField("index");
50         field.setValue(String.valueOf(study.getIndex()));
51         entry.add(field);
52         field = body.getField("class");
53         field.setValue("Study");
54         entry.add(field);
55         field = body.getField("type");
56         field.setValue("");     // Reserved for configurable Study type
57         entry.add(field);
58         field = body.getField("ref");
59         field.setValue(study.getReference());
60         entry.add(field);
61         field = body.getField("area");
62         field.setValue(study.getVisibility().toString());
63         entry.add(field);
64         field = body.getField("state");
65         field.setValue(study.getProgressState().toString());
66         entry.add(field);
67         field = body.getField("author");
68         field.setValue(study.getAuthor().toString());
69         entry.add(field);
70         field = body.getField("title");
71         field.setValue(study.getTitle());
72         entry.add(field);
73         field = body.getField("contents");
74         field.setValue(study.getTitle());
75         entry.add(field);
76
77 //      Addition of optional fields
78         setActorsOf(study);
79         setContextAt(study.getSteps());
80       }
81       private Entry (KnowledgeElement kelm) throws CorruptIndexException, LockObtainFailedException, IOException
82       {
83         super(index, analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
84
85 //      Addition of mandatory fields
86         entry = new org.apache.lucene.document.Document();        
87         Field field;
88         field = body.getField("index");
89         field.setValue(String.valueOf(kelm.getIndex()));
90         entry.add(field);
91         field = body.getField("class");
92         field.setValue("KnowledgeElement");
93         entry.add(field);
94         field = body.getField("type");
95         field.setValue(kelm.getType().getName());
96         entry.add(field);
97         field = body.getField("ref");
98         field.setValue(kelm.getReference());
99         entry.add(field);
100         field = body.getField("area");
101         field.setValue(kelm.getVisibility().toString());
102         entry.add(field);
103         field = body.getField("state");
104         field.setValue(kelm.getProgressState().toString());
105         entry.add(field);
106         field = body.getField("author");
107         field.setValue(kelm.getAuthor().toString());
108         entry.add(field);
109         field = body.getField("title");
110         field.setValue(kelm.getTitle());
111         entry.add(field);
112         field = body.getField("contents");
113         field.setValue(kelm.getTitle());
114         entry.add(field);
115
116 //TODO: Addition of optional fields
117         Scenario scene = kelm.getOwnerScenario();
118         Study    study = scene.getOwnerStudy();
119         setActorsOf(study);  // For restricting the visibility of knowledges attached to private studies
120         setContextAt(study.getSteps());
121         setContextAt(scene.getSteps());
122       }
123       private void add () throws CorruptIndexException, IOException
124       {
125         addDocument(entry);
126 //      Save the new entry
127         optimize();          // Should be called before committing the index
128         close();             // Commits the index
129       }
130       private void update () throws CorruptIndexException, IOException
131       {
132         String value = entry.getField("ref").stringValue();   // Only field with unique value
133         Term   term  = new Term("ref").createTerm(value);
134         updateDocument(term, entry);
135 //      Save the updated entry
136         optimize();          // Should be called before committing the index
137         close();             // Commits the index
138       }
139       private void setContextAt (Step[] step)
140       {
141         for (int i=0; i<step.length; i++) {
142           List<SimulationContext> contexts = step[i].getAllSimulationContexts();
143           for (Iterator<SimulationContext> j=contexts.iterator(); j.hasNext();) {
144             SimulationContext  context = j.next();
145             String             type    = String.valueOf(context.getType().getIndex());
146             String             value   = context.getValue();
147             entry.add( new Field(type, value, Field.Store.NO, Field.Index.NOT_ANALYZED) );
148           }
149         }
150       }
151       private void setActorsOf (Study study)
152       {
153         Set<User> actors = study.getActors();
154         for (Iterator<User> i=actors.iterator(); i.hasNext(); ) {
155           String value = i.next().toString();
156           entry.add( new Field("actor", value, Field.Store.NO, Field.Index.NOT_ANALYZED) );
157         }
158       }
159     }      
160     public static class ObjectProxy implements Proxy, Serializable {
161 //  --------------------------------------------------------------
162       private int           rid;
163       private String        sid;
164       private ProgressState state;
165       private String        title;
166       private String        type;
167       private String        name;
168       private static final long serialVersionUID = -4386494192709562221L;
169           
170       public ObjectProxy (org.apache.lucene.document.Document ludoc) {
171         rid   = Integer.valueOf(ludoc.get("index"));
172         sid   = ludoc.get("ref");
173         state = ProgressState.valueOf(ludoc.get("state"));
174         title = ludoc.get("title");
175         name  = ludoc.get("author");
176       }
177       public String getAuthorName () {
178         return name;
179       }
180       public Integer getIndex () {
181         return rid;
182       }
183       public ProgressState getProgressState () {
184         return state;
185       }
186       public String getReference () {
187         return sid;
188       }
189       public String getTitle () {
190         return title;
191       }
192       public String getType () {
193         return type;
194       }
195     }
196
197 //  ==============================================================================================================================
198 //  Construction
199 //  ==============================================================================================================================
200     
201     protected static void create () throws IOException {
202 //  -------------------------------
203       Directory   index  = FSDirectory.open(Database.getRepositoryIndexDirectory());
204           IndexWriter writer = new IndexWriter(index, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);
205           writer.close();                                    // ====  Creates an empty index
206     }
207
208     protected Index () throws IOException {
209 //  ------------------
210       File indir = Database.getRepositoryIndexDirectory();
211       index = FSDirectory.open(indir);
212       body  = new org.apache.lucene.document.Document();
213       body.add( new Field("index",   "", Field.Store.YES, Field.Index.NOT_ANALYZED) );
214       body.add( new Field("class",   "", Field.Store.NO,  Field.Index.NOT_ANALYZED) );
215       body.add( new Field("type",    "", Field.Store.YES, Field.Index.NOT_ANALYZED) );
216       body.add( new Field("ref",     "", Field.Store.YES, Field.Index.NOT_ANALYZED) );
217       body.add( new Field("area",    "", Field.Store.NO,  Field.Index.NOT_ANALYZED) );
218       body.add( new Field("state",   "", Field.Store.YES, Field.Index.NOT_ANALYZED) );
219       body.add( new Field("author",  "", Field.Store.YES, Field.Index.NOT_ANALYZED) );
220       body.add( new Field("title",   "", Field.Store.YES, Field.Index.NOT_ANALYZED) );
221       body.add( new Field("contents","", Field.Store.NO,  Field.Index.ANALYZED) );
222     }
223
224 //  ==============================================================================================================================
225 //  Member functions
226 //  ==============================================================================================================================
227
228     protected void add (Study study) throws IOException {
229 //  --------------------------------
230       Index.Entry entry = new Entry(study);
231       entry.add();
232       if (logger.isInfoEnabled()) {
233           logger.info("Study \"" + study.getIndex() + "\" indexed.");
234       }
235     }
236
237     protected void add (KnowledgeElement kelm) throws IOException {
238 //  ------------------------------------------
239           Index.Entry entry = new Entry(kelm);
240       entry.add();
241           if (logger.isInfoEnabled()) {
242           logger.info("Knowledge \"" + kelm.getIndex() + "\" indexed.");
243           }
244     }
245
246     protected boolean exists () {
247 //  ---------------------------
248       try {
249                 return IndexReader.indexExists(index);
250       }
251       catch (IOException error) {
252         error.printStackTrace();
253         return false;
254         }
255     }
256
257     protected void update (Study study) throws IOException {
258 //  -----------------------------------
259           Index.Entry entry = new Entry(study);
260           entry.update();
261       if (logger.isInfoEnabled()) {
262           logger.info("Study \"" + study.getIndex() + "\" re-indexed.");
263       }
264     }
265
266     protected void update (KnowledgeElement kelm) throws IOException {
267 //  ---------------------------------------------
268           Index.Entry entry = new Entry(kelm);
269           entry.update();
270       if (logger.isInfoEnabled()) {
271           logger.info("Knowledge \"" + kelm.getIndex() + "\" re-indexed.");
272       }
273     }
274 }