4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
SugDateAttribute.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: SugDateAttribute.java
5  * Description: Class representing attribute of type Date for prupose of suggestion
6  */
7 
8 package cz.vutbr.fit.knot.annotations.modules.suggestionManager.attributes;
9 
13 import java.text.SimpleDateFormat;
14 import java.util.Date;
15 import java.util.TimeZone;
16 import javax.persistence.DiscriminatorValue;
17 import javax.persistence.Entity;
18 import javax.persistence.NamedQueries;
19 import javax.persistence.NamedQuery;
20 
21 /**
22  * @file SugDateAttribute.java
23  *
24  * @brief Class representing attribute of type Date for prupose of suggestion
25  */
26 
27 /**
28  * Class representing attribute of type Date for prupose of suggestion
29  *
30  * @brief Class representing attribute of type Date for prupose of suggestion
31  */
32 @Entity
33 @DiscriminatorValue("Date")
34 @NamedQueries({
35  @NamedQuery(name = "SuggestionAttribute.findByDateValue", query = "SELECT a FROM SuggestionAttribute a WHERE a.dateValue = :dateValue"),
36 })
37 public class SugDateAttribute extends SugDateTimeAttribute {
38 
39  /**
40  * Constructor
41  */
42  public SugDateAttribute() {
43  }
44 
45  /**
46  * Constructor
47  *
48  * @param name Name of the attribute
49  * @param value Value of the attribute
50  * @param refSuggestion Suggestion to which this attribute belongs
51  */
52  public SugDateAttribute(String name, Date value, Suggestion refSuggestion) {
53  this.name = name;
54  this.simpleType = "Date";
55  this.dateValue = value;
56  this.refSuggestion = refSuggestion;
57  }
58 
59  /**
60  * Constructor
61  *
62  * @param name Name of the attribute
63  * @param value Value of the attribute
64  * @param refSuggestion Suggestion to which this attribute belongs
65  * @param priority Priority of attribute
66  */
67  public SugDateAttribute(String name, Date value, Suggestion refSuggestion, Integer priority) {
68  this.name = name;
69  this.simpleType = "Date";
70  this.dateValue = value;
71  this.refSuggestion = refSuggestion;
72  this.priority = priority;
73  }
74 
75  /**
76  * Parses provided value and sets that value as a value of attribute
77  *
78  * @param value Date value in format according to RFC 3339
79  */
80  @Override
81  public void setRawValue(String value) throws IllegalArgumentException {
82  String zone = Util.parseTimeZoneID(value);
83  String originalOffset = TimeZone.getDefault().getID();
84  if(zone != null){
85  TimeZone.setDefault(TimeZone.getTimeZone("GMT" + zone));
86  this.stringValue = zone;
87  } else {
88  this.boolVAlue = true; // suppress time zone
89  }
90  try {
91  this.dateValue = Util.parseDate(value);
92  } catch (Exception e) {
93  try {
94  this.dateValue = Util.parseRFC3339Date(value);
95  } catch (Exception ex) {
96  try {
97  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
98  this.dateValue = sdf.parse(value);
99  } catch (Exception exx) {
100  throw new IllegalArgumentException("Value " + value + " is not a valid date");
101  }
102  }
103  }
104  if(zone != null){
105  TimeZone.setDefault(TimeZone.getTimeZone(originalOffset));
106  }
107  } // setRawValue()
108 
109  /**
110  * Converts date to string in format according to RFC 3339
111  *
112  * @return Returns date in string in format according to RFC 3339
113  */
114  @Override
115  protected String xmlFormatValue() {
116  if(this.stringValue != null){
117  String originalOffset = TimeZone.getDefault().getID();
118  TimeZone.setDefault(TimeZone.getTimeZone("GMT" + this.stringValue));
119  String ret = Util.toRFC3339DateOnly(dateValue);
120  TimeZone.setDefault(TimeZone.getTimeZone(originalOffset));
121  return ret;
122  }
123  if (this.boolVAlue != null && this.boolVAlue == true) {
124  return Util.toRFC3339DateOnlyWTZ(this.dateValue);
125  }
126  return Util.toRFC3339DateOnly(this.dateValue);
127  }
128 
129  /**
130  * Gets URI addres in ontology for this type of attribute
131  *
132  * @return Return URI addres in ontology for this type of attribute.
133  */
134  @Override
135  public String getTypeOntologyUri(){
136  return Constants.DATE_URI;
137  }
138 }
Class representing attribute of type Date for prupose of suggestion.
SugDateAttribute(String name, Date value, Suggestion refSuggestion, Integer priority)
Class representing attribute of type DateTime prupose of suggestion.
Class representing suggestion of annotation.
Definition: Suggestion.java:87
Utility class (manipulates RFC 3339 dates)
Definition: Util.java:29