4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
EditUserASSetting.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Author: Ing. Jaroslav Dytrych idytrych@fit.vutbr.cz
4  * File: EditUserASSetting.java
5  * Description: Backbean for pages for adding, editing and deleting of user annotation style settings
6  */
7 
8 /**
9  * @file EditUserASSetting.java
10  *
11  * @brief Backbean for pages for adding, editing and deleting of user annotation style settings
12  */
13 package cz.vutbr.fit.knot.annotations.web;
14 
18 import java.io.Serializable;
19 import java.util.logging.Level;
20 import java.util.logging.Logger;
21 import javax.faces.bean.ManagedBean;
22 import javax.faces.bean.ViewScoped;
23 
24 /**
25  * Backbean for pages for adding, editing and deleting of user annotation style settings
26  *
27  * @brief Backbean for pages for adding, editing and deleting of user annotation style settings
28  * @author Martin Petr (xpetrm05)
29  */
30 @ManagedBean
31 @ViewScoped
32 public class EditUserASSetting implements Serializable{
33  /**
34  * Name of user annotation style setting
35  */
36  private String annotTypeName = null;
37 
38  /**
39  * Name of annotation type of user annotation style setting
40  */
41  private String settingName = null;
42 
43  /**
44  * Edited setting or setting to be deleted
45  */
46  private Settings setting = null;
47 
48  /**
49  * Red component of annotation background color
50  */
51  private Integer bgRed = 255;
52 
53  /**
54  * Green component of annotation background color
55  */
56  private Integer bgGreen = 255;
57 
58  /**
59  * Blue component of annotation background color
60  */
61  private Integer bgBlue = 255;
62 
63  /**
64  * Alpha of annotation background color
65  */
66  private Float bgAlpha = (float)1;
67 
68  /**
69  * Red component of annotation font color
70  */
71  private Integer textRed = 0;
72 
73  /**
74  * Green component of annotation font color
75  */
76  private Integer textGreen = 0;
77 
78  /**
79  * Green component of annotation font color
80  */
81  private Integer textBlue = 0;
82 
83  /**
84  * Indicates that the text is bold
85  */
86  private Boolean bold = false;
87 
88  /**
89  * Indicates that the text is italic
90  */
91  private Boolean italic = false;
92 
93  /**
94  * Indicates that the text is underlined
95  */
96  private Boolean underlined = false;
97 
98  /**
99  * Error message displayed in form
100  */
101  private String errorMessage = "";
102 
103  /** Index of background color in setting value string*/
104  public static final int BACKGROUND_COLOR = 0;
105  /** Index of font color in setting value string*/
106  public static final int FONT_COLOR = 1;
107  /** Index of bold text flag in setting value string*/
108  public static final int FONT_BOLD = 2;
109  /** Index of italic text flag in setting value string*/
110  public static final int FONT_ITALIC = 3;
111  /** Index of underlined text flag in setting value string*/
112  public static final int FONT_UNDERLINED = 4;
113  /** Length of color prefix*/
114  public static final int PREFIX_LENGTH = 5;
115  /** Index of setting name in name string */
116  public static final int SETTING_NAME = 0;
117  /** Index of annot type name in name string */
118  public static final int ANNOT_NAME = 1;
119  /** Index of red content of color in color string*/
120  public static final int RED = 0;
121  /** Index of green content of color in color string*/
122  public static final int GREEN = 1;
123  /** Index of blue content of color in color string*/
124  public static final int BLUE = 2;
125  /** Index of alpha content of color in color string*/
126  public static final int ALPHA = 3;
127 
128  /**
129  * Constructor
130  */
131  public EditUserASSetting() {
132 
133  Settings backup = null;
134  if (SessionManager.getSession().getFormBackup() != null) {
135  if (SessionManager.getSession().getFormBackup().getClass().getName().endsWith("Settings")) {
136  backup = (Settings) SessionManager.getSession().getFormBackup();
137  setting = backup;
138  return;
139  }
140  }
141 
142  if(SessionManager.getSession().getSavedValue() != null){
143  parseValue(SessionManager.getSession().getSavedValue());
144  }else if(SessionManager.getSession().getEditedSetting() != null){
145  parseValue(SessionManager.getSession().getEditedSetting().getValue());
146  }
147 
148  if(SessionManager.getSession().getEditedSetting() != null)
149  setting = SessionManager.getSession().getEditedSetting();
150 
151  if(SessionManager.getSession().getSavedName() != null){
152  parseName(SessionManager.getSession().getSavedName());
153  }else if(SessionManager.getSession().getEditedSetting() != null){
154  parseName(SessionManager.getSession().getEditedSetting().getName());
155  }else{
156  annotTypeName = "";
157  settingName = "";
158  }
159 
160  if(SessionManager.getSession().getAnnotationTypeAddress() != null){
161  String[] result = SessionManager.getSession().getAnnotationTypeAddress().split("/");
162  annotTypeName = result[result.length-1];
163  }
164  }
165 
166  /**
167  * Gets name of user annotation style setting
168  *
169  * @return Returns name of user annotation style setting
170  */
171  public String getAnnotTypeName() {
172  return annotTypeName;
173  }
174 
175  /**
176  * Sets name of user annotation style setting
177  *
178  * @param annotTypeName Name of user annotation style setting
179  */
180  public void setAnnotTypeName(String annotTypeName) {
181  this.annotTypeName = annotTypeName;
182  }
183 
184  /**
185  * Gets name of annotation type of user annotation style setting
186  *
187  * @return Returns name of annotation type of user annotation style setting
188  */
189  public String getSettingName() {
190  return settingName;
191  }
192 
193  /**
194  * Sets name of annotation type of user annotation style setting
195  *
196  * @param settingName Name of annotation type of user annotation style setting
197  */
198  public void setSettingName(String settingName) {
199  this.settingName = settingName;
200  }
201 
202  /**
203  * Gets red component of annotation background color as string
204  *
205  * @return Returns red component of annotation background color as string
206  */
207  public String getBgRed() {
208  return bgRed.toString();
209  }
210 
211  /**
212  * Sets red component of annotation background color as string
213  *
214  * @param bgRed Red component of annotation background color as string
215  */
216  public void setBgRed(String bgRed) {
217  try{
218  this.bgRed = Integer.parseInt(bgRed);
219  }
220 
221  catch(Exception e){
222  this.bgRed = -1;
223  }
224  }
225 
226  /**
227  * Gets green component of annotation background color as string
228  *
229  * @return Returns green component of annotation background color as string
230  */
231  public String getBgGreen() {
232  return bgGreen.toString();
233  }
234 
235  /**
236  * Sets green component of annotation background color as string
237  *
238  * @param bgGreen Green component of annotation background color as string
239  */
240  public void setBgGreen(String bgGreen) {
241  this.bgGreen = Integer.parseInt(bgGreen);
242  }
243 
244  /**
245  * Gets blue component of annotation background color as string
246  *
247  * @return Returns blue component of annotation background color as string
248  */
249  public String getBgBlue() {
250  return bgBlue.toString();
251  }
252 
253  /**
254  * Sets blue component of annotation background color as string
255  *
256  * @param bgBlue Blue component of annotation background color as string
257  */
258  public void setBgBlue(String bgBlue) {
259  this.bgBlue = Integer.parseInt(bgBlue);
260  }
261 
262  /**
263  * Gets alpha component of annotation background color as string
264  *
265  * @return Returns alpha component of annotation background color as string
266  */
267  public String getBgAlpha() {
268  Float tmpFloat = bgAlpha*100;
269  Integer tmpInt = tmpFloat.intValue();
270  return tmpInt.toString();
271  }
272 
273  /**
274  * Sets alpha component of annotation background color as string
275  *
276  * @param bgAlpha Alpha component of annotation background color as string
277  */
278  public void setBgAlpha(String bgAlpha) {
279  Float tmpFloat = Float.parseFloat(bgAlpha);
280  this.bgAlpha = tmpFloat / 100;
281  }
282 
283  /**
284  * Gets red component of annotation font color as string
285  *
286  * @return Returns red component of annotation font color as string
287  */
288  public String getTextRed() {
289  return textRed.toString();
290  }
291 
292  /**
293  * Sets red component of annotation font color as string
294  *
295  * @param textRed Red component of annotation font color as string
296  */
297  public void setTextRed(String textRed) {
298  this.textRed = Integer.parseInt(textRed);
299  }
300 
301  /**
302  * Gets green component of annotation font color as string
303  *
304  * @return Returns green component of annotation font color as string
305  */
306  public String getTextGreen() {
307  return textGreen.toString();
308  }
309 
310  /**
311  * Sets green component of annotation font color as string
312  *
313  * @param textGreen Green component of annotation font color as string
314  */
315  public void setTextGreen(String textGreen) {
316  this.textGreen = Integer.parseInt(textGreen);
317  }
318 
319  /**
320  * Gets blue component of annotation font color as string
321  *
322  * @return Returns blue component of annotation font color as string
323  */
324  public String getTextBlue() {
325  return textBlue.toString();
326  }
327 
328  /**
329  * Sets blue component of annotation font color as string
330  *
331  * @param textBlue Blue component of annotation font color as string
332  */
333  public void setTextBlue(String textBlue) {
334  this.textBlue = Integer.parseInt(textBlue);
335  }
336 
337  /**
338  * Gets flag that indicates the font is bold
339  *
340  * @return Returns flag that indicates the font is bold
341  */
342  public Boolean getBold() {
343  return bold;
344  }
345 
346  /**
347  * Sets flag that indicates the font is bold
348  *
349  * @param bold Flag that indicates the font is bold
350  */
351  public void setBold(Boolean bold) {
352  this.bold = bold;
353  }
354 
355  /**
356  * Gets flag that indicates the font is italic
357  *
358  * @return Returns flag that indicates the font is italic
359  */
360  public Boolean getItalic() {
361  return italic;
362  }
363 
364  /**
365  * Sets flag that indicates the font is italic
366  *
367  * @param italic Flag that indicates the font is italic
368  */
369  public void setItalic(Boolean italic) {
370  this.italic = italic;
371  }
372 
373  /**
374  * Gets flag that indicates the font is underlined
375  *
376  * @return Returns flag that indicates the font is underlined
377  */
378  public Boolean getUnderlined() {
379  return underlined;
380  }
381 
382  /**
383  * Sets flag that indicates the font is underlined
384  *
385  * @param underlined Flag that indicates the font is underlined
386  */
387  public void setUnderlined(Boolean underlined) {
388  this.underlined = underlined;
389  }
390 
391  /**
392  * Gets error message displayed in form
393  *
394  * @return Returns error message displayed in form
395  */
396  public String getErrorMessage() {
397  return errorMessage;
398  }
399 
400  /**
401  * Sets error message to be displayed in form
402  *
403  * @param errorMessage Error message to be displayed in form
404  */
405  public void setErrorMessage(String errorMessage) {
406  this.errorMessage = errorMessage;
407  }
408 
409  /**
410  * Action listener for save button in the page for editing of user setting
411  *
412  * @return Returns page outcome (identificator of next page or null to stay here)
413  */
414  public String btnSaveAction(){
415  boolean errorOccurred = false;
416  errorMessage = "";
417 
418  if(annotTypeName.isEmpty()){
419  errorMessage = errorMessage + MessageProvider.getMessage("userASSettingsAnnotError") + " ";
420  errorOccurred = true;
421  }
422 
423  if(settingName.isEmpty()){
424  errorMessage = errorMessage + MessageProvider.getMessage("userASSettingsNameError") + " ";
425  errorOccurred = true;
426  }
427 
428  if((bgRed < 0) || (bgRed > 255)){
429  errorMessage = errorMessage + MessageProvider.getMessage("userASSettingsColorError") + " ";
430  errorOccurred = true;
431  }
432 
433  if((bgGreen < 0) || (bgGreen > 255)){
434  errorMessage = errorMessage + MessageProvider.getMessage("userASSettingsColorError") + " ";
435  errorOccurred = true;
436  }
437 
438  if((bgBlue < 0) || (bgBlue > 255)){
439  errorMessage = errorMessage + MessageProvider.getMessage("userASSettingsColorError") + " ";
440  errorOccurred = true;
441  }
442 
443  if((bgAlpha < 0) || (bgAlpha > 100)){
444  errorMessage = errorMessage + MessageProvider.getMessage("userASSettingsAlphaError") + " ";
445  errorOccurred = true;
446  }
447 
448  if((textRed < 0) || (textRed > 255)){
449  errorMessage = errorMessage + MessageProvider.getMessage("userASSettingsColorError") + " ";
450  errorOccurred = true;
451  }
452 
453  if((textGreen < 0) || (textGreen > 255)){
454  errorMessage = errorMessage + MessageProvider.getMessage("userASSettingsColorError") + " ";
455  errorOccurred = true;
456  }
457 
458  if((textBlue < 0) || (textBlue > 255)){
459  errorMessage = errorMessage + MessageProvider.getMessage("userASSettingsColorError") + " ";
460  errorOccurred = true;
461  }
462 
463  if(errorOccurred){
464  errorMessage = MessageProvider.getMessage("error") + " " + errorMessage;
465  return null;
466  }
467 
468  setting.setName(makeName());
469  setting.setValue(makeValue());
470  setting.setUser(SessionManager.getSession().getEditedUser());
471 
472  if ((AppBean.getPersistenceManager().saveEntityChanges(setting)) == null) {
473  errorMessage = MessageProvider.getMessage("changesNSDatabaseFailure");
475  String msg = "Persisting of changes in the user settings failed.";
476  Logger.getLogger(EditUser.class.getName()).log(Level.SEVERE, msg);
477  }
478  return null;
479  }
480 
481  SessionManager.getSession().setAnnotationTypeAddress(null);
482  SessionManager.getSession().setSavedValue(null);
483  SessionManager.getSession().setSavedName(null);
484  SessionManager.getSession().setEditedSetting(null);
485  return "saved";
486  }
487 
488  /**
489  * Action listener for seve button on page for adding new user setting
490  *
491  * @return Returns page outcome (identificator of next page or null to stay here)
492  */
493  public String btnSaveNewAction(){
494  boolean errorOccurred = false;
495  errorMessage = "";
496 
497  if(annotTypeName.isEmpty()){
498  errorMessage = errorMessage + MessageProvider.getMessage("userASSettingsAnnotError") + " ";
499  errorOccurred = true;
500  }
501 
502  if(settingName.isEmpty()){
503  errorMessage = errorMessage + MessageProvider.getMessage("userASSettingsNameError") + " ";
504  errorOccurred = true;
505  }
506 
507  if((bgRed < 0) || (bgRed > 255)){
508  errorMessage = errorMessage + MessageProvider.getMessage("userASSettingsColorError") + " ";
509  errorOccurred = true;
510  }
511 
512  if((bgGreen < 0) || (bgGreen > 255)){
513  errorMessage = errorMessage + MessageProvider.getMessage("userASSettingsColorError") + " ";
514  errorOccurred = true;
515  }
516 
517  if((bgBlue < 0) || (bgBlue > 255)){
518  errorMessage = errorMessage + MessageProvider.getMessage("userASSettingsColorError") + " ";
519  errorOccurred = true;
520  }
521 
522  if((bgAlpha < 0) || (bgAlpha > 100)){
523  errorMessage = errorMessage + MessageProvider.getMessage("userASSettingsAlphaError") + " ";
524  errorOccurred = true;
525  }
526 
527  if((textRed < 0) || (textRed > 255)){
528  errorMessage = errorMessage + MessageProvider.getMessage("userASSettingsColorError") + " ";
529  errorOccurred = true;
530  }
531 
532  if((textGreen < 0) || (textGreen > 255)){
533  errorMessage = errorMessage + MessageProvider.getMessage("userASSettingsColorError") + " ";
534  errorOccurred = true;
535  }
536 
537  if((textBlue < 0) || (textBlue > 255)){
538  errorMessage = errorMessage + MessageProvider.getMessage("userASSettingsColorError") + " ";
539  errorOccurred = true;
540  }
541 
542  if(errorOccurred){
543  errorMessage = MessageProvider.getMessage("error") + " " + errorMessage;
544  return null;
545  }
546 
547  setting = new Settings(SessionManager.getSession().getEditedUser(),makeName(),makeValue());
548 
549  if (AppBean.getPersistenceManager().persistEntity(setting)) {
550  errorMessage = MessageProvider.getMessage("newUserSettingDBFailure");
552  String msg = "Persisting of new user settings failed.";
553  Logger.getLogger(EditUser.class.getName()).log(Level.SEVERE, msg);
554  }
555  return null;
556  }
557 
558  SessionManager.getSession().setAnnotationTypeAddress(null);
559  SessionManager.getSession().setSavedValue(null);
560  SessionManager.getSession().setSavedName(null);
561  SessionManager.getSession().setEditedSetting(null);
562  return "saved";
563  }
564 
565  /**
566  * Returns "typeSelect"
567  * @return
568  */
569  public String btnSelectAction() {
570  SessionManager.getSession().setAnnotationTypeAddress(null);
571  SessionManager.getSession().setSavedValue(makeValue());
572  SessionManager.getSession().setSavedName(makeName());
573  return "typeSelect";
574  }
575 
576  /**
577  * Action listener for cancel button
578  *
579  * @return Returns page outcome (identificator of next page or null to stay here)
580  */
581  public String btnCancelAction() {
582  SessionManager.getSession().setAnnotationTypeAddress(null);
583  SessionManager.getSession().setSavedValue(null);
584  SessionManager.getSession().setSavedName(null);
585  SessionManager.getSession().setEditedSetting(null);
586  return "cancel";
587  }
588 
589  /**
590  * Action listener for delete button on deleting page.
591  *
592  * @return Returns page outcome (identificator of next page or null to stay here)
593  */
594  public String btnDeleteAction(){
595  if(AppBean.getPersistenceManager().removeEntity(setting)){
596  errorMessage = MessageProvider.getMessage("userSettingsDeleteError");
598  String msg = "DB failure during deleting of user settings.";
599  Logger.getLogger(EditSubscriptions.class.getName()).log(Level.SEVERE, msg);
600  }
601 
602  return null;
603  }
604 
605  SessionManager.getSession().setAnnotationTypeAddress(null);
606  SessionManager.getSession().setSavedValue(null);
607  SessionManager.getSession().setSavedName(null);
608  SessionManager.getSession().setFormBackup(null);
609  SessionManager.getSession().setEditedSetting(null); // clean up variable in session
610  return "cancel";
611  }
612 
613  /**
614  * Method parse input value string to element parts and save them to object properties
615  *
616  * @param value Input value string to parse
617  */
618  private void parseValue(String value){
619  String[] result = value.split(";");
620  if(result.length == 5){
621 
622  String bgColor = result[BACKGROUND_COLOR].substring(PREFIX_LENGTH,result[BACKGROUND_COLOR].indexOf(")"));
623  String[] resultBg = bgColor.split(",");
624  if(resultBg.length == 4){
625  bgRed = Integer.parseInt(resultBg[RED]);
626  bgGreen = Integer.parseInt(resultBg[GREEN]);
627  bgBlue = Integer.parseInt(resultBg[BLUE]);
628  bgAlpha = Float.parseFloat(resultBg[ALPHA]);
629  }
630 
631  String fontColor = result[FONT_COLOR].substring(PREFIX_LENGTH,result[FONT_COLOR].indexOf(")"));
632  String[] resultFont = fontColor.split(",");
633  if(resultFont.length == 4){
634  textRed = Integer.parseInt(resultFont[RED]);
635  textGreen = Integer.parseInt(resultFont[GREEN]);
636  textBlue = Integer.parseInt(resultFont[BLUE]);
637  }
638 
639  bold = Boolean.valueOf(result[FONT_BOLD]);
640  italic = Boolean.valueOf(result[FONT_ITALIC]);
641  underlined = Boolean.valueOf(result[FONT_UNDERLINED]);
642  }
643  }
644 
645  /**
646  * Method parse name value string to element parts and save them to object properties
647  *
648  * @param name Input name string to parse
649  */
650  private void parseName(String name){
651  String[] result = name.split(":");
652  if(result.length == 2){
653  settingName = result[SETTING_NAME];
654  annotTypeName = result[ANNOT_NAME];
655  }else if(result.length == 1){
656  settingName = result[SETTING_NAME];
657  annotTypeName = "";
658  }
659  }
660 
661  /**
662  * Method make from objects properties value string
663  *
664  * @return Returns value string
665  */
666  private String makeValue(){
667  return "rgba(" + bgRed.toString() + "," + bgGreen.toString() + "," + bgBlue.toString() + "," + bgAlpha.toString() + ");" +
668  "rgba(" + textRed.toString() + "," + textGreen.toString() + "," + textBlue.toString() + ",1);" +
669  bold.toString() + ";" + italic.toString() + ";" + underlined.toString();
670  }
671 
672  /**
673  * Method make from objects properties name string
674  *
675  * @return Returns name string
676  */
677  private String makeName(){
678  return settingName + ":" + annotTypeName;
679  }
680 
681 }
Class for manipulating with session.
Singleton for storing global variables.
Definition: AppBean.java:47
Backbean for pages for adding, editing and deleting of user annotation style settings.
Class representing parameter of user settings.
Definition: Settings.java:45