]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/SearchStudyAction.java
Salome HOME
070c1de8773c6e23fbcf6a4f13adf64b56e839c5
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / SearchStudyAction.java
1 package org.splat.simer;
2
3 import java.util.LinkedHashMap;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.splat.dal.bo.kernel.User;
8 import org.splat.dal.bo.som.ProgressState;
9 import org.splat.dal.bo.som.SimulationContext;
10 import org.splat.dal.bo.som.SimulationContextType;
11 import org.splat.dal.bo.som.Study;
12 import org.splat.kernel.InvalidPropertyException;
13 import org.splat.service.SearchService;
14 import org.splat.service.technical.ProjectSettingsService;
15 import org.splat.wapp.Constants;
16
17 /**
18  * Search studies form action.
19  */
20 public class SearchStudyAction extends AbstractSearchBaseAction {
21
22         /**
23          * Serial version ID.
24          */
25         private static final long serialVersionUID = -1910481357051393077L;
26
27         /**
28          * "In-Work", "In-Draft", "In-Check"...
29          */
30         private String _state = null;
31         /**
32          * Study reference.
33          */
34         private String _reference = null;
35         /**
36          * Injected project settings service.
37          */
38         private ProjectSettingsService _projectSettings;
39         /**
40          * Injected search service.
41          */
42         private SearchService _searchService;
43
44         enum UserAction {
45                 refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext
46         }
47
48         // ==============================================================================================================================
49         // Action methods
50         // ==============================================================================================================================
51
52         /**
53          * Initialize study search form.
54          * 
55          * @return SUCCESS if no exception, otherwise return ERROR
56          */
57         public String doInitialize() {
58                 String res = SUCCESS;
59                 try {
60                         loadFilter();
61                         doSearch();
62
63                         // Final initialization of the form
64                         setCandidates();
65                         setContextTypeOptions(getInvolvedContexts());
66                         initializationFullScreenContext(Constants.OPEN, Constants.NONE,
67                                         Constants.OPEN);
68
69                 } catch (Exception error) {
70                         // No need to roll back the transaction as it is read only
71                         LOG.error("Reason: ", error);
72
73                         initializationScreenContext(Constants.NONE);
74
75                         res = ERROR;
76                 }
77                 return res;
78         }
79
80         /**
81          * {@inheritDoc}
82          * 
83          * @see org.splat.simer.AbstractSearchBaseAction#doSearch()
84          */
85         @Override
86         protected String doSearch() throws InvalidPropertyException {
87                 Map<String, Object> session = getSession();
88                 Study.Properties sprop = new Study.Properties();
89
90                 // Search matching all criteria
91                 if (!this._state.equals("ANY")) {
92                         sprop.setState(ProgressState.valueOf(this._state));
93                 }
94                 if (getWords().length() > 0) {
95                         sprop.setTitle(getWords());
96                 }
97                 if (_reference.length() > 0) {
98                         sprop.setReference(_reference);
99                 }
100                 if (_context.size() > 0) {
101                         sprop.setSimulationContexts(_context);
102                 }
103                 int index = Integer.valueOf(_author);
104                 if (index > 0) {
105                         User him = getUserService().selectUser(index);
106                         sprop.setManager(him);
107                 }
108                 sprop.setActor(getConnectedUser());
109
110                 _result = getSearchService().selectStudiesWhere(
111                                 "all".equals(getCriteriaMatch()),
112                                 "all".equals(getContextMatch()), sprop);
113                 session.put(RESULT_KEY, _result); // For redisplaying the page without re-executing the search
114                 return "refresh";
115         }
116
117         // ==============================================================================================================================
118         // Getters
119         // ==============================================================================================================================
120
121         public String getReference() {
122                 return _reference;
123         }
124
125         public String getState() {
126                 return _state;
127         }
128
129         public void setReference(final String value) {
130                 this._reference = value;
131         }
132
133         public void setState(final String value) {
134                 this._state = value;
135         }
136
137         @Override
138         protected List<SimulationContextType> getInvolvedContexts() {
139                 List<ProjectSettingsService.Step> steps = getProjectSettings()
140                                 .getStepsOf(Study.class);
141                 ProjectSettingsService.Step[] number = steps
142                                 .toArray(new ProjectSettingsService.Step[steps.size()]);
143
144                 return getSimulationContextService().selectTypesOf(number);
145         }
146
147         @Override
148         @SuppressWarnings("unchecked")
149         protected void loadFilter() {
150                 Map<String, Object> session = getSession();
151                 Map<String, Object> filter = (Map<String, Object>) session
152                                 .get("study.filter"); // A default filter is supposed being set at start
153
154                 setCriteriaMatch((String) filter.get("matchamong"));
155                 setContextMatch((String) filter.get("matcontext"));
156                 _state = (String) filter.get("state");
157                 _author = (String) filter.get("author");
158                 _reference = (String) filter.get("reference");
159                 setWords((String) filter.get("title"));
160                 _context = (List<SimulationContext>) filter.get("context");
161         }
162
163         @Override
164         @SuppressWarnings("unchecked")
165         protected void saveFilter() {
166                 Map<String, Object> session = getSession();
167                 Map<String, Object> filter = (Map<String, Object>) session
168                                 .get("study.filter"); // A default filter is supposed being set at start
169
170                 filter.put("matchamong", getCriteriaMatch());
171                 filter.put("matcontext", getContextMatch());
172                 filter.put("state", this._state);
173                 filter.put("author", this._author);
174                 filter.put("reference", this._reference);
175                 filter.put("title", getWords());
176
177                 _context = (List<SimulationContext>) filter.get("context"); // Only criteria not part of the form
178
179         }
180
181         /**
182          * Get match options.
183          * 
184          * @return array of options with key and value properties
185          */
186         public Map<String, String> getStateOptions() {
187                 Map<String, String> options = new LinkedHashMap<String, String>();
188                 addStateOption(options, "ANY");
189                 if (getConnectedUser() != null) {
190                         addStateOption(options, ProgressState.inWORK.name());
191                         addStateOption(options, ProgressState.inDRAFT.name());
192                         addStateOption(options, ProgressState.inCHECK.name());
193                 }
194                 addStateOption(options, ProgressState.APPROVED.name());
195                 addStateOption(options, ProgressState.TEMPLATE.name());
196                 return options;
197         }
198
199         /**
200          * Add a state and its translation to the list (map) of options.
201          * 
202          * @param options
203          *            the map of options
204          * @param state
205          *            the state to add
206          */
207         private void addStateOption(final Map<String, String> options,
208                         final String state) {
209                 options.put(state, getText("criterion." + state.toLowerCase()));
210         }
211
212         /**
213          * Get the searchService.
214          * 
215          * @return the searchService
216          */
217         public SearchService getSearchService() {
218                 return _searchService;
219         }
220
221         /**
222          * Set the searchService.
223          * 
224          * @param searchService
225          *            the searchService to set
226          */
227         public void setSearchService(final SearchService searchService) {
228                 _searchService = searchService;
229         }
230
231         /**
232          * Get project settings.
233          * 
234          * @return Project settings service
235          */
236         private ProjectSettingsService getProjectSettings() {
237                 return _projectSettings;
238         }
239
240         /**
241          * Set project settings service.
242          * 
243          * @param projectSettingsService
244          *            project settings service
245          */
246         public void setProjectSettings(
247                         final ProjectSettingsService projectSettingsService) {
248                 _projectSettings = projectSettingsService;
249         }
250 }