4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
DateTimeAttribute.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: DateTimeAttribute.java
5  * Description: Class representing attribute of type DateTime
6  */
7 
8 package cz.vutbr.fit.knot.annotations.entity.attribute;
9 
12 import java.util.Date;
13 import java.util.TimeZone;
14 import javax.persistence.DiscriminatorValue;
15 import javax.persistence.Entity;
16 
17 /**
18  * @file DateTimeAttribute.java
19  *
20  * @brief Class representing attribute of type DateTime
21  */
22 
23 /**
24  * Class representing attribute of type DateTime
25  *
26  * @brief Class representing attribute of type DateTime
27  */
28 @Entity
29 @DiscriminatorValue("DateTime")
30 public class DateTimeAttribute extends BaseAttribute {
31 
32  /**
33  * Gets value of the attribute
34  *
35  * @return value of the attribute
36  */
37  @Override
38  public Object getValue() {
39  return dateValue;
40  }
41 
42  /**
43  * Sets value of the attribute. Value should already be a native java Date value.
44  *
45  * @param value new value of the attribute
46  */
47  @Override
48  public void setValue(Object value) {
49  this.dateValue = (Date) value;
50  }
51 
52  /**
53  * Converts date to string in format according to RFC 3339
54  *
55  * @return Date in string in format according to RFC 3339
56  */
57  @Override
58  protected String xmlFormatValue() {
59  if (dateValue == null) {
60  return "";
61  }
62  if(this.stringValue != null){
63  String originalOffset = TimeZone.getDefault().getID();
64  TimeZone.setDefault(TimeZone.getTimeZone("GMT" + this.stringValue));
65  String ret = Util.toRFC3339Date(dateValue);
66  TimeZone.setDefault(TimeZone.getTimeZone(originalOffset));
67  return ret;
68  }
69  else{
70  return Util.toRFC3339Date(dateValue);
71  }
72  }
73 
74  /**
75  * Parses provided value and sets that value as a value of attribute
76  *
77  * @param value Date time value in format according to RFC 3339
78  */
79  @Override
80  public void setRawValue(String value) throws IllegalArgumentException {
81  if(value == null){
82  return;
83  }
84 
85  String zone = Util.parseTimeZoneID(value);
86  String originalOffset = TimeZone.getDefault().getID();
87  if(zone != null){
88  TimeZone.setDefault(TimeZone.getTimeZone("GMT" + zone));
89  this.stringValue = zone;
90  }
91  try {
92  this.dateValue = Util.parseRFC3339Date(value);
93  } catch (Exception e) {
94  throw new IllegalArgumentException("Value " + value + " is not a valid RFC3339 date time");
95  }
96  if(zone != null){
97  TimeZone.setDefault(TimeZone.getTimeZone(originalOffset));
98  }
99  }
100 
101  /**
102  * Gets URI addres in ontology for this type of attribute
103  *
104  * @return Return URI addres in ontology for this type of attribute.
105  */
106  @Override
107  public String getTypeOntologyUri(){
109  }
110 } // class DateTimeAttribute
Base class representing attribute of annotation.
Utility class (manipulates RFC 3339 dates)
Definition: Util.java:29