4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
DateAttribute.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Author: Ing. Jaroslav Dytrych idytrych@fit.vutbr.cz, Jan Planer xplane01@stud.fit.vutbr.cz
4  * File: DateAttribute.java
5  * Description: Class representing attribute of type Date
6  */
7 
8 package cz.vutbr.fit.knot.annotations.entity.attribute;
9 
12 import java.text.SimpleDateFormat;
13 import java.util.TimeZone;
14 import javax.persistence.DiscriminatorValue;
15 import javax.persistence.Entity;
16 import javax.persistence.NamedQueries;
17 import javax.persistence.NamedQuery;
18 
19 /**
20  * @file DateAttribute.java
21  *
22  * @brief Class representing attribute of type Date
23  */
24 
25 /**
26  * Class representing attribute of type Date
27  *
28  * @brief Class representing attribute of type Date
29  */
30 @Entity
31 @DiscriminatorValue("Date")
32 @NamedQueries({
33  @NamedQuery(name = "Attribute.findByDateValue", query = "SELECT a FROM Attribute a WHERE a.dateValue = :dateValue"),
34 })
35 public class DateAttribute extends DateTimeAttribute {
36 
37  /**
38  * Parses provided value and sets that value as a value of attribute
39  *
40  * @param value Date value in format according to RFC 3339
41  */
42  @Override
43  public void setRawValue(String value) throws IllegalArgumentException {
44  if(value == null){
45  return;
46  }
47 
48  String zone = Util.parseTimeZoneID(value);
49  String originalOffset = TimeZone.getDefault().getID();
50  if(zone != null){
51  TimeZone.setDefault(TimeZone.getTimeZone("GMT" + zone));
52  this.stringValue = zone;
53  this.boolVAlue = false;
54  } else {
55  this.boolVAlue = true; // suppress time zone
56  }
57  try {
58  this.dateValue = Util.parseDate(value);
59  } catch (Exception e) {
60  try {
61  this.dateValue = Util.parseRFC3339Date(value);
62  } catch (Exception ex) {
63  try {
64  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
65  this.dateValue = sdf.parse(value);
66  } catch (Exception exx) {
67  throw new IllegalArgumentException("Value " + value + " is not a valid date");
68  }
69  }
70  }
71  if(zone != null){
72  TimeZone.setDefault(TimeZone.getTimeZone(originalOffset));
73  }
74  } // setRawValue()
75 
76  /**
77  * Converts date to string in format according to RFC 3339
78  *
79  * @return Returns date in string in format according to RFC 3339
80  */
81  @Override
82  protected String xmlFormatValue() {
83  if(this.stringValue != null){
84  String originalOffset = TimeZone.getDefault().getID();
85  TimeZone.setDefault(TimeZone.getTimeZone("GMT" + this.stringValue));
86  String ret = Util.toRFC3339DateOnly(dateValue);
87  TimeZone.setDefault(TimeZone.getTimeZone(originalOffset));
88  return ret;
89  }
90  if (this.boolVAlue != null && this.boolVAlue == true) {
91  return Util.toRFC3339DateOnlyWTZ(this.dateValue);
92  }
93  return Util.toRFC3339DateOnly(this.dateValue);
94  }
95 
96  /**
97  * Gets URI addres in ontology for this type of attribute
98  *
99  * @return Return URI addres in ontology for this type of attribute.
100  */
101  @Override
102  public String getTypeOntologyUri(){
103  return Constants.DATE_URI;
104  }
105 } // class DateAttribute
Class representing attribute of type Date.
Utility class (manipulates RFC 3339 dates)
Definition: Util.java:29