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