]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/SearchKnowledgeAction.java
Salome HOME
Refactoring: kernel and som are moved to Siman-Common.
[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
19
20 public class SearchKnowledgeAction extends SearchBaseAction {
21
22     private String                       visibility = null;   // "Private", "Public", "All"
23     private String                       typid      = null;   // Knowledge type index when among all
24     private String                       matchamong = null;   // "all" or "any"
25     private String                       matcontext = null;   // "all" or "any"
26     private String                       refid      = null;   // Knowledge reference  when among ref
27     private String                       words      = null;   // Full text search words
28     private List<KnowledgeElementType>   types;            // Available knowledge types filter (initialized below)
29
30     private static final long serialVersionUID = -3104321907432838476L;
31
32 //  ==============================================================================================================================
33 //  Action methods
34 //  ==============================================================================================================================
35
36         public String doInitialize () {
37 //  -----------------------------
38       Session      connex  = Database.getSession();
39       Transaction  transax = connex.beginTransaction();      
40       try {
41         loadFilter();          
42         doSearch();
43
44 //      Final initialization of the form
45         types = KnowledgeElement.selectTypesWhere(ProgressState.APPROVED);
46         setCandidates();
47         setContextTypeOptions(getInvolvedContexts());
48
49         transax.commit();
50         return SUCCESS;
51       }
52       catch (Exception error) {
53 //          No need to roll back the transaction as it is read only
54         logger.error("Reason: ", error);
55         return ERROR;
56       }
57         }
58
59         protected String doSearch () throws InvalidPropertyException {
60 //  ----------------------------
61       Map<String, Object> session = getSession();
62       User                user    = getConnectedUser();
63
64       KnowledgeElement.Properties  sprop = new KnowledgeElement.Properties();
65
66 //    Search matching all criteria
67         sprop.setType( KnowledgeElement.selectType(Integer.valueOf(typid)) );
68         if (words.length() > 0)          sprop.setTitle(words);
69         if (refid.length() > 0)          sprop.setReference(refid);
70         if (context.size() > 0)          sprop.setSimulationContexts(context);
71         int index = Integer.valueOf(author);
72         if (index > 0) {
73           User him = UserDirectory.selectUser(index);
74           sprop.setAuthor(him);
75         }
76 //      Set of the visibility
77         if (visibility.equals("all")) {
78           KnowledgeElement.Properties  other = sprop.copy();
79
80           other.setVisibility(Visibility.PUBLIC);
81           sprop.setVisibility(Visibility.PRIVATE);
82           sprop.setActor(user);
83
84           result = Database.selectKnowledgeElementsWhere(sprop, other);
85         }
86         else {
87           Visibility reparea = null;
88           if (visibility.equals("onlypublic")) reparea = Visibility.PUBLIC;
89           else                                 reparea = Visibility.valueOf(visibility);
90           sprop.setVisibility(reparea);
91           if (reparea == Visibility.PRIVATE) sprop.setActor(user);
92
93           result = Database.selectKnowledgeElementsWhere(sprop);
94       }
95       session.put("search.result", result);     // For redisplaying the page without re-executing the search
96       return "refresh";
97         }
98
99 //  ==============================================================================================================================
100 //  Getters
101 //  ==============================================================================================================================
102
103     public String getContextMatch () {
104 //  --------------------------------
105       return matcontext;
106     }
107     public String getCriteriaMatch () {
108 //  ---------------------------------
109       return matchamong;
110     }
111     public List<KnowledgeElementType> getKnowledgeTypes () {
112 //  ------------------------------------------------------
113       return types;
114     }
115     public String getReference () {
116 //  -----------------------------
117       return refid;
118     }
119     public String getState () {
120 //  ------------------------
121       return typid;
122     }
123     public String getVisibility () {
124 //  ------------------------------
125       return visibility;
126     }
127     public String getWords () {
128 //  -------------------------
129       return words;
130     }
131
132 //  ==============================================================================================================================
133 //  Setters
134 //  ==============================================================================================================================
135
136     public void setContextMatch (String value) {
137 //  ------------------------------------------
138       this.matcontext = value;
139     }
140     public void setCriteriaMatch (String value) {
141 //  -------------------------------------------
142       this.matchamong = value;
143     }
144     public void setReference (String value) {
145 //  ---------------------------------------
146       this.refid = value;
147     }
148     public void setState (String value) {
149 //  ----------------------------------
150       this.typid = value;
151     }
152     public void setVisibility (String value) {
153 //  ----------------------------------------
154       this.visibility = value;
155     }
156     public void setWords (String value) {
157 //  -----------------------------------
158       this.words = value;
159     }
160
161 //  ==============================================================================================================================
162 //  Implementation of abstract services
163 //  ==============================================================================================================================
164
165         protected List<SimulationContextType> getInvolvedContexts () {
166 //  ------------------------------------------------------------
167       return SimulationContext.selectAllTypes();
168         }
169
170     @SuppressWarnings("unchecked")
171         protected void loadFilter () {
172 //  ----------------------------
173       Map<String,Object> session = getSession();
174       User               user    = getConnectedUser();
175       Map<String,Object> filter  = (Map<String, Object>)session.get("knowledge.filter");   // A default filter is supposed being set at start
176
177       visibility = (String)filter.get("visibility");
178       matchamong = (String)filter.get("matchamong");
179       matcontext = (String)filter.get("matcontext");
180       typid      = (String)filter.get("type");
181       author     = (String)filter.get("author");
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         visibility = "onlypublic";
188       }
189     }
190
191         @SuppressWarnings("unchecked")
192         protected void saveFilter () {
193 //  ----------------------------
194       Map<String,Object> session = getSession();
195       Map<String,Object> filter  = (Map<String, Object>)session.get("knowledge.filter");   // A default filter is supposed being set at start
196
197       filter.put("visibility", this.visibility);
198       filter.put("matchamong", this.matchamong);
199       filter.put("matcontext", this.matcontext);
200       filter.put("type",   this.typid);
201       filter.put("author", this.author);
202       filter.put("reference", "");
203       filter.put("title", this.words);
204
205       context = (List<SimulationContext>)filter.get("context");  // Only criteria not part of the form
206
207 //    Initialization required by all do functions
208       types = KnowledgeElement.selectTypesWhere(ProgressState.APPROVED);
209         }
210 }