Project

General

Profile

1
/*
2
 * Copyright 2013 gathanas.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

    
17
package eu.dnetlib.espas.util;
18

    
19
import java.net.URI;
20
import java.sql.Timestamp;
21
import javax.xml.bind.JAXBElement;
22
import javax.xml.bind.JAXBException;
23
import javax.xml.bind.Unmarshaller;
24
import org.apache.log4j.Logger;
25
import org.apache.log4j.Priority;
26
import org.geotoolkit.gml.xml.v321.TimeInstantType;
27
import org.geotoolkit.gml.xml.v321.TimePeriodType;
28
import org.opengis.temporal.Period;
29
import org.opengis.temporal.TemporalPrimitive;
30
import org.w3c.dom.Element;
31
import org.w3c.dom.Node;
32

    
33
/**
34
 *
35
 * @author gathanas
36
 */
37

    
38
/**
39
 * This class supports the extraction of temporal details defined in the espas
40
 * model and encoded in GML v3.2 format
41
 */
42
class GMLTemporalTransformationHandler extends GMLTransformationHandler {
43

    
44
    private static final Logger _logger = Logger.getLogger(GMLTemporalTransformationHandler.class);
45

    
46
    public GMLTemporalTransformationHandler() {
47
        super();
48
    }
49

    
50
    public String getTemporalStartTime(Node gmlNode) {
51
        String result = "";
52
        try {
53
            _logger.log(Priority.DEBUG,"GetTemporal start input node is :\n" + MetadataHandler.serializeNode(gmlNode));
54
            if (!(gmlNode instanceof Element) || !((Element) gmlNode).getNamespaceURI().equalsIgnoreCase(SUPPORT_GML_SPEC))
55
                return null;
56

    
57
            Unmarshaller unMarshaller = this.acquireUnMarshaller();
58

    
59
            Object gmlObject = unMarshaller.unmarshal(gmlNode);
60

    
61
            if (gmlObject instanceof JAXBElement)
62
                gmlObject = ((JAXBElement) gmlObject).getValue();
63
            //        if this is not a geometry object then an emty result is returned
64
            if ((gmlObject instanceof TemporalPrimitive)){
65
                if ((gmlObject instanceof TimeInstantType))
66
                        result=  ((TimeInstantType)gmlObject).getTimePosition().getDateTime().toString();
67
                else if(gmlObject instanceof TimePeriodType)
68
                    result = getPeriodTime(((TimePeriodType)gmlObject), TimePeriodConstant.start);
69
            }
70
            //      free resources by releasing unmashaller instance
71
            releaseUnMarshaller(unMarshaller);
72
           _logger.log(Priority.INFO,"GML geometry is :\n" + result);
73

    
74
        } catch (Exception ex) {
75
            _logger.log(Priority.ERROR, null, ex);
76
        } finally {
77
            return result;
78
        }
79
    }
80

    
81
    public String getTemporalEndTime(Node gmlNode) {
82
        String result = "";
83
        try {
84

    
85
            _logger.log(Priority.DEBUG,"GetTemporal end input node is :\n" + MetadataHandler.serializeNode(gmlNode));
86
            if (!(gmlNode instanceof Element) /*|| !((Element) gmlNode).getNamespaceURI().equalsIgnoreCase(SUPPORT_GML_SPEC)*/)
87
                return null;
88

    
89
            Unmarshaller unMarshaller = this.acquireUnMarshaller();
90

    
91
            Object gmlObject = unMarshaller.unmarshal(gmlNode);
92

    
93
            if (gmlObject instanceof JAXBElement)
94
                gmlObject = ((JAXBElement) gmlObject).getValue();
95
            //        if this is not a temporal period object then an emty result is returned
96
            if ((gmlObject instanceof TemporalPrimitive)){
97
                if ((gmlObject instanceof TimeInstantType))
98
                        result=  ((TimeInstantType)gmlObject).getTimePosition().getDateTime().toString();
99
                else if(gmlObject instanceof TimePeriodType)
100
                    result = getPeriodTime(((TimePeriodType)gmlObject), TimePeriodConstant.end);
101
                }
102
            //      free resources by releasing unmashaller instance
103
            releaseUnMarshaller(unMarshaller);
104
            _logger.log(Priority.INFO,"GML geometry is :\n" + result);
105

    
106
        } catch (JAXBException ex) {
107
                _logger.log(Priority.ERROR, null, ex);
108

    
109
        } finally {
110
            return result;
111
        }
112
    }
113

    
114
    
115
    /** Parses the given gml:TimeInstant element and extracts the given content in a string format that can be inserted into a 
116
     * postgres db.
117
     */
118
    public String getTimeInstant(Node gmlNode) {
119
        String result = "";
120
        try {
121

    
122
            if (!(gmlNode instanceof Element) || !((Element) gmlNode).getNamespaceURI().equalsIgnoreCase(SUPPORT_GML_SPEC))
123
                return null;
124

    
125
            Unmarshaller unMarshaller = this.acquireUnMarshaller();
126

    
127
            Object gmlObject = unMarshaller.unmarshal(gmlNode);
128

    
129
            if (gmlObject instanceof JAXBElement)
130
                gmlObject = ((JAXBElement) gmlObject).getValue();
131
            //        if this is not a time instant object then an emty result is returned
132
            if ((gmlObject instanceof TimeInstantType))
133
                        result=  ((TimeInstantType)gmlObject).getTimePosition().getDateTime().toString();
134

    
135
            //      free resources by releasing unmashaller instance
136
            releaseUnMarshaller(unMarshaller);
137
            _logger.log(Priority.INFO,"GML geometry is :\n" + result);
138

    
139
        } catch (Exception ex) {
140
                _logger.log(Priority.ERROR, "Exception while processing time instant", ex);
141

    
142
        } finally {
143
            return result;
144
        }
145
    }
146

    
147
    
148
    /** This function supports the Time Period element parsing. It extracts the timeperiod end time in case this is specified inside a period element or the 
149
     * the time defined in a time instance element so as to comply to the application constraints agreed in espas. 
150
     */
151
    
152
   public String getTimePeriodEnd(Node timePeriodType) {
153
        String result = "";
154
        try {
155

    
156
            if (!(timePeriodType instanceof Element) || !((Element) timePeriodType).getNamespaceURI().equalsIgnoreCase(SUPPORT_GML_SPEC))
157
                return null;
158

    
159
            Unmarshaller unMarshaller = this.acquireUnMarshaller();
160

    
161
            Object gmlObject = unMarshaller.unmarshal(timePeriodType);
162

    
163
            if (gmlObject instanceof JAXBElement)
164
                gmlObject = ((JAXBElement) gmlObject).getValue();
165
            //        if this is not a time instant object then an emty result is returned
166
            if ((gmlObject instanceof TimeInstantType))
167
                        result=  ((TimeInstantType)gmlObject).getTimePosition().getDateTime().toString();
168
            else if(gmlObject instanceof TimePeriodType)
169
                result = getPeriodTime(((TimePeriodType)gmlObject), TimePeriodConstant.end);
170

    
171
            //      free resources by releasing unmashaller instance
172
            releaseUnMarshaller(unMarshaller);
173
            _logger.log(Priority.INFO,"GML geometry is :\n" + result);
174

    
175
        } catch (JAXBException ex) {
176
                _logger.log(Priority.ERROR, "Exception while processing time instant", ex);
177

    
178
        } finally {
179
            return result;
180
        }
181
    }
182

    
183
    /** This function supports the Time Period element parsing. It extracts the timeperiod start time in case this is specified inside a period element or the 
184
     * the time defined in a time instance element so as to comply to the application constraints agreed in espas. 
185
     */
186
   public String getTimePeriodStart(Node timePeriodType) {
187
        String result = "";
188
        try {
189

    
190
            if (!(timePeriodType instanceof Element) || !((Element) timePeriodType).getNamespaceURI().equalsIgnoreCase(SUPPORT_GML_SPEC))
191
                return null;
192

    
193
            Unmarshaller unMarshaller = this.acquireUnMarshaller();
194

    
195
            Object gmlObject = unMarshaller.unmarshal(timePeriodType);
196

    
197
            if (gmlObject instanceof JAXBElement)
198
                gmlObject = ((JAXBElement) gmlObject).getValue();
199
            //        if this is not a time instant object then an emty result is returned
200
            if ((gmlObject instanceof TimeInstantType))
201
                        result=  ((TimeInstantType)gmlObject).getTimePosition().getDateTime().toString();
202
            else if(gmlObject instanceof TimePeriodType)
203
                result = getPeriodTime(((TimePeriodType)gmlObject), TimePeriodConstant.start);
204

    
205
            //      free resources by releasing unmashaller instance
206
            releaseUnMarshaller(unMarshaller);
207
            _logger.log(Priority.INFO,"GML geometry is :\n" + result);
208

    
209
        } catch (JAXBException ex) {
210
                _logger.log(Priority.ERROR, "Exception while processing time instant", ex);
211

    
212
        } finally {
213
            return result;
214
        }
215
    }
216
   
217
   
218
   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
219
   ///                  Private functions used for assistive tasks
220
   enum TimePeriodConstant {start, end};
221
   
222
   private String getPeriodTime(TimePeriodType period, TimePeriodConstant periodConst){
223
       String output ="";
224
       switch(periodConst){
225
           case end:
226
               if (period.getEnd() != null)
227
                   output = period.getEnd().getTimeInstant().getTimePosition().getDateTime().toString();
228
               else if (period.getEndPosition() != null)
229
                   output = period.getEndPosition().getDateTime().toString();
230
               else if (period.getEnding() != null)
231
                   output = period.getEnding().getPosition().getDateTime().toString();
232
               break;
233
               
234
           case start:
235
               if (period.getEnd() != null)
236
                   output = period.getBegin().getTimeInstant().getTimePosition().getDateTime().toString();                        
237
               else if (period.getBeginPosition() != null)
238
                   output = period.getBeginPosition().getDateTime().toString();
239
               else if (period.getBeginning() != null)
240
                   output = period.getBeginning().getPosition().getDateTime().toString();
241
               break;
242

    
243
       }
244
       
245
       return output;
246
   }
247
}
248

    
(2-2/9)