Salome HOME
SIMAN Eclipse workspace first version
[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.kernel.User;
10 import org.splat.kernel.UserDirectory;
11 import org.splat.som.Database;
12 import org.splat.som.ProgressState;
13 import org.splat.som.ProjectSettings;
14 import org.splat.som.SimulationContext;
15 import org.splat.som.SimulationContextType;
16 import org.splat.som.Study;
17 import org.splat.som.Visibility;
18 import org.splat.wapp.Item;
19 import org.splat.wapp.TabBar;
20
21
22 public class SearchStudyAction extends SearchBaseAction {
23
24     private TabBar                       area    = null;   // Active repository area
25     private String                       state   = null;   // "inPROGRESS", "inCHECK" or "END"
26     private String                       among   = null;   // "all", "mine" or "ref"
27     private String                       refid   = null;   // Study reference when among ref
28     private String                       words   = null;   // Full text search words
29
30         private static final long serialVersionUID = -1910481357051393077L;
31
32     enum UserAction { refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext }
33
34 //  ==============================================================================================================================
35 //  Action methods
36 //  ==============================================================================================================================
37
38         public String doInitialize () {
39 //  -----------------------------
40       Session      connex  = Database.getSession();
41       Transaction  transax = connex.beginTransaction();
42       try {
43         loadFilter();
44         if (newarea != null) {                        // New selected repository area
45           area.selects(newarea);
46         }
47         doSearch();
48
49 //      Final initialization of the form
50         setCandidates();
51         setContextTypeOptions(getInvolvedContexts());
52
53         transax.commit();
54         return SUCCESS;
55       }
56       catch (Exception error) {
57 //      No need to roll back the transaction as it is read only
58         logger.error("Reason: ", error);
59         return ERROR;
60       }
61     }
62
63         protected String doSearch () throws InvalidPropertyException {
64 //  ----------------------------
65       Map<String, Object> session = getSession();
66       User                user    = getConnectedUser();
67
68       Study.Properties  sprop = new Study.Properties();
69       Study.Properties  other = null;
70
71 //    Set of the selected repository area
72       String     selectab = area.getSelection().toUpperCase();
73       Visibility reparea  = Visibility.valueOf(selectab);
74       sprop.setVisibility(reparea);
75
76 //    Search from a given reference
77       if (among.equals("ref")) {
78         if (refid.length() == 0) {
79           getSession().remove("search.result");                      // The current result is obsolete
80           return "wait";
81         }
82         sprop.setReference(refid);
83         if (reparea == Visibility.PRIVATE) {                         // Restriction to studies in which the connected user is involved
84           other = sprop.copy().setActor(user);
85           sprop.setManager(user);
86         }
87       } else {
88 //    Search from other available criteria
89         if (this.state != null) {
90           ProgressState  state = ProgressState.APPROVED;             // Trick for the Public and Reference areas to not share the APPROVED state 
91           if (!this.state.equals("ARCHIVED")) state = ProgressState.valueOf(this.state);
92           sprop.setState(state);
93         }
94         if (words.length() > 0)        sprop.setTitle(words);
95         if (context.size() > 0)        sprop.setSimulationContexts(context);
96         if      (among.equals("mine")) sprop.setManager(user);
97         else if (among.equals("his")) {
98           User him = UserDirectory.selectUser(Integer.valueOf(author));
99           sprop.setManager(him);
100           if (reparea == Visibility.PRIVATE) sprop.setActor(user);
101         } else //among.equals("all")
102           if (reparea == Visibility.PRIVATE) {
103                 other = sprop.copy().setActor(user);
104                 sprop.setManager(user);
105           }
106       }
107       if (other == null) result = Database.selectStudiesWhere(sprop);
108       else               result = Database.selectStudiesWhere(sprop, other);
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 getArea () {
119 //  ------------------------
120       return area.getSelection();
121     }
122     public String getOwner () {
123 //  -------------------------
124       return among;
125     }
126     public String getReference () {
127 //  -----------------------------
128       return refid;
129     }
130     public String getState () {
131 //  -------------------------
132       return state;
133     }
134     public List<Item> getTabs () {
135 //  ----------------------------
136       return area.asList();
137     }
138     public String getWords () {
139 //  -------------------------
140       return words;
141     }
142
143 //  ==============================================================================================================================
144 //  Setters
145 //  ==============================================================================================================================
146
147     public void setOwner (String value) {
148 //  -----------------------------------
149       this.among = 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 setWords (String value) {
160 //  -----------------------------------
161       this.words = value;
162     }
163
164 //  ==============================================================================================================================
165 //  Implementation of abstract services
166 //  ==============================================================================================================================
167
168         protected List<SimulationContextType> getInvolvedContexts () {
169 //  ------------------------------------------------------------
170       List<ProjectSettings.Step>  steps  = ProjectSettings.getStepsOf(Study.class);
171       ProjectSettings.Step[]      number = steps.toArray(new ProjectSettings.Step[steps.size()]);
172       
173       return SimulationContext.selectTypesOf(number);
174         }
175
176     @SuppressWarnings("unchecked")
177         protected void loadFilter () {
178 //  ----------------------------
179       Map<String,Object> session = getSession();
180       User                user    = getConnectedUser();
181       Map<String,Object> filter  = (Map<String, Object>)session.get("study.filter");   // A default filter is supposed being set at start
182
183       area    = (TabBar)filter.get("area");
184       state   = (String)filter.get("state");
185       among   = (String)filter.get("owner");
186       author  = (String)filter.get("author");
187       refid   = (String)filter.get("reference");
188       words   = (String)filter.get("title");
189       context = (List<SimulationContext>)filter.get("context");
190
191       if (user == null) {
192         area.disables("private");
193         if (area.getSelection().equals("private")) area.selects("public");
194         if (among.equals("mine")) among = "all";
195       }
196     }
197     
198     @SuppressWarnings("unchecked")
199         protected void saveFilter () {
200 //  ----------------------------
201       Map<String, Object> session = getSession();
202       Map<String, Object> filter  = (Map<String, Object>)session.get("study.filter");   // A default filter is supposed being set at start
203
204       area = (TabBar)filter.get("area");   // The area being not an input, it is null when submitting the form
205       if (among.equals("ref")) {
206         filter.put("owner", "ref");
207         filter.put("reference", this.refid);
208       } else {
209         filter.put("state",  this.state);
210         filter.put("owner",  this.among);
211         filter.put("author", this.author);
212         filter.put("reference", "");
213         filter.put("title",  this.words);
214         
215         context = (List<SimulationContext>)filter.get("context");  // Only criteria not part of the form
216       }
217     }
218 }