4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
DurationAttribute.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: DurationAttribute.java
5  * Description: Class representing attribute of type Duration
6  */
7 
8 package cz.vutbr.fit.knot.annotations.entity.attribute;
9 
11 import javax.persistence.DiscriminatorValue;
12 import javax.persistence.Entity;
13 import javax.xml.datatype.DatatypeFactory;
14 import javax.xml.datatype.Duration;
15 import org.joda.time.Period;
16 import org.joda.time.format.ISOPeriodFormat;
17 import org.joda.time.format.PeriodFormatter;
18 import org.joda.time.format.PeriodFormatterBuilder;
19 
20 /**
21  * @file DurationAttribute.java
22  *
23  * @brief Class representing attribute of type Duration
24  */
25 
26 /**
27  * Class representing attribute of type Duration
28  *
29  * @brief Class representing attribute of type Duration
30  */
31 @Entity
32 @DiscriminatorValue("Duration")
33 public class DurationAttribute extends BaseAttribute {
34 
35  /**
36  * Gets value of the attribute
37  *
38  * @return object of type Duration
39  */
40  @Override
41  public Object getValue() {
42  try {
43  // XML Schema - we extended so it is commented out
44  // return (Object) DatatypeFactory.newInstance().newDuration(this.stringValue);
45 
46  // RFC 3339
47  PeriodFormatter pf = new PeriodFormatterBuilder().append(ISOPeriodFormat.standard()).toFormatter();
48  Period p = pf.parsePeriod(this.stringValue);
49  return (Object) p;
50  } catch (Exception e) {
51  return null;
52  }
53  }
54 
55  /**
56  * Returns string representation of duration according to RFC 3339
57  *
58  * @return string representation of duration according to RFC 3339
59  */
60  @Override
61  public String getStringValue() {
62  return this.stringValue;
63  }
64 
65  /**
66  * Sets value of the attribute. Value should already be a native java Duration value.
67  *
68  * @param value object of type Duration
69  */
70  @Override
71  public void setValue(Object value) {
72  this.stringValue = ((Duration) value).toString();
73  }
74 
75  /**
76  * Parses provided value and sets that value as a value of attribute
77  *
78  * @param value new value of the attribute in raw form from XML
79  */
80  @Override
81  public void setRawValue(String value) throws IllegalArgumentException {
82  if(value == null){
83  return;
84  }
85 
86  /*
87  * This is a strict variant for XML Scchema but we need to extend to RFC 3339
88  *
89  try {
90  Duration d = DatatypeFactory.newInstance().newDuration(value);
91  this.stringValue = d.toString();
92  } catch (Exception e) {
93  throw new IllegalArgumentException("Value " + value
94  + " is not a valid Duration value according to http://www.w3.org/TR/xmlschema-2/#duration");
95  }*/
96 
97  /*
98  * Parse and set using format RFC 3339
99  */
100  try {
101  PeriodFormatter pf = new PeriodFormatterBuilder().append(ISOPeriodFormat.standard()).toFormatter();
102  Period p = pf.parsePeriod(value);
103  this.stringValue = p.toString();
104  } catch (Exception e) {
105  throw new IllegalArgumentException("Value " + value
106  + " is not a valid Duration value according to RFC 3339");
107  }
108 
109  } // setRawValue()
110 
111  /**
112  * Checks if attribute value is empty or default value
113  *
114  * @return true, if attribute value is empty
115  */
116  @Override
117  public boolean isEmpty() {
118  return stringValue == null || stringValue.equals("");
119  }
120 
121  /**
122  * Gets URI addres in ontology for this type of attribute
123  *
124  * @return Return URI addres in ontology for this type of attribute.
125  */
126  @Override
127  public String getTypeOntologyUri(){
128  return Constants.DURATION_URI;
129  }
130 } // class DurationAttribute
Base class representing attribute of annotation.