Salome HOME
Fix for a study creation with a new simulation context when no "product" context...
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / EditStepCommentAction.java
1 /*****************************************************************************
2  * Company         EURIWARE
3  * Application     SIMAN
4  * File            $Id$
5  * Creation date   Nov 21, 2012
6  * @author         $Author$
7  * @version        $Revision$
8  *****************************************************************************/
9
10 package org.splat.simer;
11
12 import java.util.Date;
13 import java.util.List;
14
15 import org.splat.exception.InvalidParameterException;
16 import org.splat.service.StepService;
17 import org.splat.service.dto.StepCommentDTO;
18 import org.splat.wapp.PopupMenu;
19 /**
20  * Step comment creation/modification action.
21  *
22  */
23 public class EditStepCommentAction extends DisplayStudyStepAction {
24
25         /**
26          * Serial version ID.
27          */
28         private static final long serialVersionUID = 6467920934724352021L;
29
30         /**
31          * Step comments.
32          */
33         private List<StepCommentDTO> _comments = null;
34
35         /**
36          * The value of the comment.
37          */
38         private String _commentValue;
39
40         /**
41          * The value of the comment.
42          */
43         private String _commentToEditValue;
44
45         /**
46          * The title of the comment.
47          */
48         private String _commentTitle;
49
50         /**
51          * Step comment id.
52          */
53         private Long _commentId;
54         
55         /**
56          * Comment popup menu.
57          */
58         private transient PopupMenu _commentPopup = null;
59
60         /**
61          * Injected Step Service.
62          */
63         private StepService _stepService;
64
65         // ==============================================================================================================================
66         // Action methods
67         // ==============================================================================================================================
68
69         /**
70          * Display step comments.
71          * @return SUCCESS
72          */
73         public String doDisplayComments() {
74                 try {
75                         _comments = getStepService().getStepComments(getOpenStudy().getSelectedStep());
76                         _commentPopup = getApplicationSettings().getPopupMenu("comment");
77                         
78                         for(StepCommentDTO comment : _comments) {
79                                 comment.getUser().setDisplayName(
80                                                 comment.getUser().getDisplayName());
81                         }
82                 } catch(InvalidParameterException exception) {
83                         _comments = null;
84                         LOG.debug("Error while trying to load comments: " + exception.getMessage());
85                 }
86                 return SUCCESS;
87         }
88         
89         /**
90          * Creation of a new comment.
91          * @return SUCCESS
92          */
93         public String doCreate() {
94                 if ("true".equals(getWriteAccess()) && getUserRights().canCreateDocument()
95                                 && _commentValue!=null  && _commentValue.length()>0
96                                 && _commentTitle!=null  && _commentTitle.length()>0) {
97
98                         StepCommentDTO stepCommentDTO = new StepCommentDTO(
99                                         null,   //id must be null
100                                         _commentValue,
101                                         getOpenStudy().getSelectedStep().getOwner().getIndex(),
102                                         getOpenStudy().getSelectedStep().getNumber(),
103                                         new Date(),     //current date and time
104                                         getConnectedUser().getRid(),
105                                         null,   //userName is unnecessary
106                                         _commentTitle
107                                 );
108                         try {
109                                 getStepService().addStepComment(stepCommentDTO);
110                         } catch (InvalidParameterException error) {
111                                 LOG.debug("Error while trying to add comment: " + error.getMessage());
112                         }
113                 }
114                 doDisplayComments();
115                 return SUCCESS;
116         }
117         
118         /**
119          * Edit a comment.
120          * @return SUCCESS
121          */
122         public String doEdit() {
123                 try {
124                         if(_stepService.isCommentMadeByUser(_commentId, getConnectedUser().getIndex())) {
125                                 getStepService().editStepComment(_commentId, _commentToEditValue, _commentTitle);
126                         }
127                 } catch(InvalidParameterException error) {
128                         LOG.debug("Error while trying to remove comment: " + error.getMessage());
129                 }
130                 doDisplayComments();
131                 return SUCCESS;
132         }
133         
134         /**
135          * Removal of a comment.
136          * @return SUCCESS
137          */
138         public String doRemove() {
139                 try {
140                         if(_stepService.isCommentMadeByUser(_commentId, getConnectedUser().getIndex())) {
141                                 getStepService().removeStepComment(_commentId);
142                         }
143                 } catch(InvalidParameterException error) {
144                         LOG.debug("Error while trying to remove comment: " + error.getMessage());
145                 }
146                 doDisplayComments();
147                 return SUCCESS;
148         }
149
150         /**
151          * Get the commentValue.
152          * @return the commentValue
153          */
154         public String getCommentValue() {
155                 return _commentValue;
156         }
157
158         /**
159          * Set the commentValue.
160          * @param commentValue the commentValue to set
161          */
162         public void setCommentValue(final String commentValue) {
163                 _commentValue = commentValue;
164         }
165
166         /**
167          * Get the commentTitle.
168          * @return the commentTitle
169          */
170         public String getCommentTitle() {
171                 return _commentTitle;
172         }
173
174         /**
175          * Set the commentTitle.
176          * @param commentTitle the commentTitle to set
177          */
178         public void setCommentTitle(final String commentTitle) {
179                 _commentTitle = commentTitle;
180         }
181
182         /**
183          * Get the comments.
184          * @return the comments
185          */
186         public List<StepCommentDTO> getComments() {
187                 return _comments;
188         }
189
190         /**
191          * Set the comments.
192          * @param comments the comments to set
193          */
194         public void setComments(final List<StepCommentDTO> comments) {
195                 _comments = comments;
196         }
197
198         /**
199          * Get the stepService.
200          * @return the stepService
201          */
202         public StepService getStepService() {
203                 return _stepService;
204         }
205
206         /**
207          * Set the stepService.
208          * @param stepService the stepService to set
209          */
210         public void setStepService(final StepService stepService) {
211                 _stepService = stepService;
212         }
213         
214         /**
215          * Get the commentPopup. Should have this exact name since it is used in menupopup.jsp.
216          * @return the commentPopup
217          */
218         @Override
219         public PopupMenu getPopup() {
220                 return _commentPopup;
221         }
222
223         /**
224          * Get the commentId.
225          * @return the commentId
226          */
227         public Long getCommentId() {
228                 return _commentId;
229         }
230
231         /**
232          * Set the commentId.
233          * @param commentId the commentId to set
234          */
235         public void setCommentId(final Long commentId) {
236                 _commentId = commentId;
237         }
238
239         /**
240          * Get the commentToEditValue.
241          * @return the commentToEditValue
242          */
243         public String getCommentToEditValue() {
244                 return _commentToEditValue;
245         }
246
247         /**
248          * Set the commentToEditValue.
249          * @param commentToEditValue the commentToEditValue to set
250          */
251         public void setCommentToEditValue(final String commentToEditValue) {
252                 _commentToEditValue = commentToEditValue;
253         }
254 }