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