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