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