Salome HOME
Refactoring of Database, replacing SQL by DAOs calls. Methods for search by criteria...
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / SearchStudyAction.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.dal.bo.kernel.User;
10 import org.splat.kernel.UserDirectory;
11 import org.splat.dal.dao.som.Database;
12 import org.splat.dal.bo.som.ProgressState;
13 import org.splat.service.SearchService;
14 import org.splat.service.SimulationContextService;
15 import org.splat.service.technical.ProjectSettingsService;
16 import org.splat.dal.bo.som.SimulationContext;
17 import org.splat.dal.bo.som.SimulationContextType;
18 import org.splat.dal.bo.som.Study;
19 import org.splat.dal.bo.som.Visibility;
20
21
22 public class SearchStudyAction extends SearchBaseAction {
23
24     private String            visibility = null;   // "Private", "Public", "All"
25     private String            state      = null;   // "In-Work", "In-Draft", "In-Check"...
26     private String            matchamong = null;   // "all" or "any"
27     private String            matcontext = null;   // "all" or "any"
28     private String            refid      = null;   // Study reference
29     private String            words      = null;   // Full text search words
30         private SearchService _searchService;
31         private ProjectSettingsService _projectSettingsService;
32         /**
33          * Injected simulation context service.
34          */
35         private SimulationContextService _simulationContextService;
36
37         /**
38          * Serial version ID.
39          */
40         private static final long serialVersionUID = -1910481357051393077L;
41
42     enum UserAction { refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext }
43
44 //  ==============================================================================================================================
45 //  Action methods
46 //  ==============================================================================================================================
47
48     public String doInitialize () {
49 //  -----------------------------
50       Session      connex  = Database.getSession();
51       Transaction  transax = connex.beginTransaction();
52       try {
53         loadFilter();
54         doSearch();
55
56 //      Final initialization of the form
57         setCandidates();
58         setContextTypeOptions(getInvolvedContexts());
59
60         transax.commit();
61         return SUCCESS;
62       }
63       catch (Exception error) {
64 //      No need to roll back the transaction as it is read only
65         logger.error("Reason: ", error);
66         return ERROR;
67       }
68     }
69
70     protected String doSearch () throws InvalidPropertyException {
71 //  ----------------------------
72       Map<String, Object> session = getSession();
73       User                user    = getConnectedUser();
74
75       Study.Properties  sprop = new Study.Properties();
76
77 //    Search matching all criteria
78         if (!this.state.equals("ANY"))     sprop.setState( ProgressState.valueOf(this.state) );
79         if (words.length() > 0)            sprop.setTitle(words);
80         if (refid.length() > 0)            sprop.setReference(refid);
81         if (context.size() > 0)            sprop.setSimulationContexts(context);
82         int index = Integer.valueOf(author);
83         if (index > 0) {
84           User him = UserDirectory.selectUser(index);
85           sprop.setManager(him);
86         }
87 //    Set of the visibility
88         if (visibility.equals("all")) {
89           Study.Properties  other = sprop.copy();
90
91           other.setVisibility(Visibility.PUBLIC);
92           sprop.setVisibility(Visibility.PRIVATE);
93           sprop.setActor(user);
94
95           result = getSearchService().selectStudiesWhere(sprop, other);
96         }
97         else {
98           Visibility reparea = null;
99           if (visibility.equals("onlypublic")) reparea = Visibility.PUBLIC;
100           else                                 reparea = Visibility.valueOf(visibility);
101           sprop.setVisibility(reparea);
102           if (reparea == Visibility.PRIVATE) sprop.setActor(user);
103
104           result = getSearchService().selectStudiesWhere(sprop);
105       }
106       session.put("search.result", result);                          // For redisplaying the page without re-executing the search
107       return "refresh";
108     }
109
110 //  ==============================================================================================================================
111 //  Getters
112 //  ==============================================================================================================================
113
114     public String getContextMatch () {
115 //  --------------------------------
116       return matcontext;
117     }
118     public String getCriteriaMatch () {
119 //  ---------------------------------
120       return matchamong;
121     }
122     public String getReference () {
123 //  -----------------------------
124       return refid;
125     }
126     public String getState () {
127 //  -------------------------
128       return state;
129     }
130     public String getVisibility () {
131 //  ------------------------------
132       return visibility;
133     }
134     public String getWords () {
135 //  -------------------------
136       return words;
137     }
138
139 //  ==============================================================================================================================
140 //  Setters
141 //  ==============================================================================================================================
142
143     public void setContextMatch (String value) {
144 //  ------------------------------------------
145       this.matcontext = value;
146     }
147     public void setCriteriaMatch (String value) {
148 //  -------------------------------------------
149       this.matchamong = value;
150     }
151     public void setReference (String value) {
152 //  ---------------------------------------
153       this.refid = value;
154     }
155     public void setState (String value) {
156 //  -----------------------------------
157       this.state = value;
158     }
159     public void setVisibility (String value) {
160 //  ----------------------------------------
161       this.visibility = value;
162     }
163     public void setWords (String value) {
164 //  -----------------------------------
165       this.words = value;
166     }
167
168 //  ==============================================================================================================================
169 //  Implementation of abstract services
170 //  ==============================================================================================================================
171
172         protected List<SimulationContextType> getInvolvedContexts () {
173 //  ------------------------------------------------------------
174       List<ProjectSettingsService.Step>  steps  = getProjectSettings().getStepsOf(Study.class);
175       ProjectSettingsService.Step[]      number = steps.toArray(new ProjectSettingsService.Step[steps.size()]);
176       
177       return getSimulationContextService().selectTypesOf(number);
178         }
179
180     @SuppressWarnings("unchecked")
181         protected void loadFilter () {
182 //  ----------------------------
183       Map<String,Object> session = getSession();
184       User                user    = getConnectedUser();
185       Map<String,Object> filter  = (Map<String, Object>)session.get("study.filter");   // A default filter is supposed being set at start
186
187       visibility = (String)filter.get("visibility");
188       matchamong = (String)filter.get("matchamong");
189       matcontext = (String)filter.get("matcontext");
190       state      = (String)filter.get("state");
191       author     = (String)filter.get("author");
192       refid      = (String)filter.get("reference");
193       words      = (String)filter.get("title");
194       context    = (List<SimulationContext>)filter.get("context");
195
196       if (user == null) {
197         visibility = "onlypublic";
198       }
199     }
200     
201     @SuppressWarnings("unchecked")
202         protected void saveFilter () {
203 //  ----------------------------
204       Map<String, Object> session = getSession();
205       Map<String, Object> filter  = (Map<String, Object>)session.get("study.filter");   // A default filter is supposed being set at start
206
207       filter.put("visibility", this.visibility);
208       filter.put("matchamong", this.matchamong);
209       filter.put("matcontext", this.matcontext);
210       filter.put("state",      this.state);
211       filter.put("author",     this.author);
212       filter.put("reference",  this.refid);
213       filter.put("title",      this.words);
214
215       context = (List<SimulationContext>)filter.get("context");  // Only criteria not part of the form
216
217     }
218     /**
219          * Get the searchService.
220          * @return the searchService
221          */
222         public SearchService getSearchService() {
223                 return _searchService;
224         }
225
226         /**
227          * Set the searchService.
228          * @param searchService the searchService to set
229          */
230         public void setSearchService(SearchService searchService) {
231                 _searchService = searchService;
232         }
233     /**
234      * Get project settings.
235          * @return Project settings service
236          */
237         private ProjectSettingsService getProjectSettings() {
238                 return _projectSettingsService;
239         }
240
241         /**
242          * Set project settings service.
243          * @param projectSettingsService project settings service
244          */
245         public void setProjectSettings(
246                         ProjectSettingsService projectSettingsService) {
247                 _projectSettingsService = projectSettingsService;
248         }
249
250         /**
251          * Get the simulationContextService.
252          * 
253          * @return the simulationContextService
254          */
255         public SimulationContextService getSimulationContextService() {
256                 return _simulationContextService;
257         }
258
259         /**
260          * Set the simulationContextService.
261          * 
262          * @param simulationContextService
263          *            the simulationContextService to set
264          */
265         public void setSimulationContextService(
266                         SimulationContextService simulationContextService) {
267                 _simulationContextService = simulationContextService;
268         }
269 }