Salome HOME
SIMAN Eclipse workspace first version
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / SearchKnowledgeAction.java
1 package org.splat.simer;
2
3 import java.util.List;
4 import java.util.Map;
5
6 import org.hibernate.Session;
7 import org.hibernate.Transaction;
8 import org.splat.kernel.InvalidPropertyException;
9 import org.splat.kernel.User;
10 import org.splat.kernel.UserDirectory;
11 import org.splat.som.Database;
12 import org.splat.som.KnowledgeElement;
13 import org.splat.som.KnowledgeElementType;
14 import org.splat.som.ProgressState;
15 import org.splat.som.SimulationContext;
16 import org.splat.som.SimulationContextType;
17 import org.splat.som.Visibility;
18 import org.splat.wapp.Item;
19 import org.splat.wapp.TabBar;
20
21
22 public class SearchKnowledgeAction extends SearchBaseAction {
23
24     private TabBar                       area    = null;   // Active repository area
25     private String                       among   = null;   // "all", "inCHECK" or "ref"
26     private String                       typid   = null;   // Knowledge type index when among all
27     private String                       refid   = null;   // Knowledge reference  when among ref
28     private String                       words   = null;   // Full text search words
29     private List<KnowledgeElementType>   types;            // Available knowledge types filter (initialized below)
30
31     private static final long serialVersionUID = -3104321907432838476L;
32
33 //  ==============================================================================================================================
34 //  Action methods
35 //  ==============================================================================================================================
36
37         public String doInitialize () {
38 //  -----------------------------
39       Session      connex  = Database.getSession();
40       Transaction  transax = connex.beginTransaction();      
41       try {
42         loadFilter();          
43         if (newarea != null) {                        // New selected repository area
44           area.selects(newarea);
45         }
46         doSearch();
47
48 //      Final initialization of the form
49         types = KnowledgeElement.selectTypesWhere(ProgressState.APPROVED);
50         setCandidates();
51         setContextTypeOptions(getInvolvedContexts());
52
53         transax.commit();
54         return SUCCESS;
55       }
56       catch (Exception error) {
57 //          No need to roll back the transaction as it is read only
58         logger.error("Reason: ", error);
59         return ERROR;
60       }
61         }
62
63         protected String doSearch () throws InvalidPropertyException {
64 //  ----------------------------
65       Map<String, Object> session = getSession();
66       User                user    = getConnectedUser();
67
68       KnowledgeElement.Properties  sprop = new KnowledgeElement.Properties();
69       KnowledgeElement.Properties  other = null;
70
71 //    Set of the selected repository area
72       String     selectab = area.getSelection().toUpperCase();
73       Visibility reparea  = Visibility.valueOf(selectab);
74       sprop.setVisibility(reparea);
75
76 //    Search from a given reference
77       if (among.equals("ref")) {
78         if (refid.length() == 0) {
79           getSession().remove("search.result");                      // The current result is obsolete
80           return "wait";
81         }
82         sprop.setReference(refid);
83         if (reparea == Visibility.PRIVATE) {                         // Restriction to studies in which the connected user is involved
84           other = sprop.copy().setActor(user);
85           sprop.setAuthor(user);
86         }
87       } else {
88 //    Search from other available criteria
89         if (words.length() > 0)          sprop.setTitle(words);
90         if (context.size() > 0)          sprop.setSimulationContexts(context);
91         if      ( among.equals("mine") ) sprop.setAuthor(user);
92         else if ( among.equals("his") ) {
93           User him = UserDirectory.selectUser(Integer.valueOf(author));
94           sprop.setAuthor(him);
95           if (reparea == Visibility.PRIVATE) sprop.setActor(user);
96         } else {//among.equals("all")
97           KnowledgeElementType type = KnowledgeElement.selectType(Integer.valueOf(typid));
98           sprop.setType(type);
99           if (reparea == Visibility.PRIVATE) {                       // Restriction to studies in which the connected user is involved
100             other = sprop.copy().setActor(user);
101             sprop.setAuthor(user);
102           }
103         }
104       }
105       if (other == null) result = Database.selectKnowledgeElementsWhere(sprop);
106       else               result = Database.selectKnowledgeElementsWhere(sprop, other);
107
108       session.put("search.result", result);     // For redisplaying the page without re-executing the search
109       return "refresh";
110         }
111
112 //  ==============================================================================================================================
113 //  Getters
114 //  ==============================================================================================================================
115
116     public List<KnowledgeElementType> getKnowledgeTypes () {
117 //  ------------------------------------------------------
118       return types;
119     }
120     public String getOwner () {
121 //  -------------------------
122       return among;
123     }
124     public String getReference () {
125 //  -----------------------------
126       return refid;
127     }
128     public String getState () {
129 //  ------------------------
130       return typid;
131     }
132     public List<Item> getTabs () {
133 //  ----------------------------
134       return area.asList();
135     }
136     public String getWords () {
137 //  -------------------------
138       return words;
139     }
140
141 //  ==============================================================================================================================
142 //  Setters
143 //  ==============================================================================================================================
144
145     public void setOwner (String value) {
146 //  -----------------------------------
147       this.among = value;
148     }
149     public void setReference (String value) {
150 //  ---------------------------------------
151       this.refid = value;
152     }
153     public void setState (String value) {
154 //  ----------------------------------
155       this.typid = value;
156     }
157     public void setWords (String value) {
158 //  -----------------------------------
159       this.words = value;
160     }
161
162 //  ==============================================================================================================================
163 //  Implementation of abstract services
164 //  ==============================================================================================================================
165
166         protected List<SimulationContextType> getInvolvedContexts () {
167 //  ------------------------------------------------------------
168       return SimulationContext.selectAllTypes();
169         }
170
171     @SuppressWarnings("unchecked")
172         protected void loadFilter () {
173 //  ----------------------------
174       Map<String,Object> session = getSession();
175       User               user    = getConnectedUser();
176       Map<String,Object> filter  = (Map<String, Object>)session.get("knowledge.filter");   // A default filter is supposed being set at start
177
178       area    = (TabBar)filter.get("area");
179       among   = (String)filter.get("owner");
180       author  = (String)filter.get("author");
181       typid   = (String)filter.get("type");
182       refid   = (String)filter.get("reference");
183       words   = (String)filter.get("title");
184       context = (List<SimulationContext>)filter.get("context");
185
186       if (user == null) {
187         area.disables("private");
188         if (area.getSelection().equals("private")) area.selects("public");
189       }
190     }
191
192         @SuppressWarnings("unchecked")
193         protected void saveFilter () {
194 //  ----------------------------
195       Map<String,Object> session = getSession();
196       Map<String,Object> filter  = (Map<String, Object>)session.get("knowledge.filter");   // A default filter is supposed being set at start
197
198       area = (TabBar)filter.get("area");   // The area being not an input, it is null when submitting the form
199       if (among.equals("ref")) {
200         filter.put("owner", "ref");
201         filter.put("reference", this.refid);
202       } else {
203         filter.put("owner", this.among);
204         filter.put("author", this.author);
205         filter.put("type", this.typid);
206         filter.put("reference", "");
207         filter.put("title", this.words);
208
209         context = (List<SimulationContext>)filter.get("context");  // Only criteria not part of the form
210       }
211 //    Initialization required by all do functions
212       types = KnowledgeElement.selectTypesWhere(ProgressState.APPROVED);
213         }
214 }