1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2000-2015 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
*
|
6
|
* '$Author: $'
|
7
|
* '$Date: $'
|
8
|
*
|
9
|
* This program is free software; you can redistribute it and/or modify
|
10
|
* it under the terms of the GNU General Public License as published by
|
11
|
* the Free Software Foundation; either version 2 of the License, or
|
12
|
* (at your option) any later version.
|
13
|
*
|
14
|
* This program is distributed in the hope that it will be useful,
|
15
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
* GNU General Public License for more details.
|
18
|
*
|
19
|
* You should have received a copy of the GNU General Public License
|
20
|
* along with this program; if not, write to the Free Software
|
21
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
22
|
*/
|
23
|
|
24
|
package edu.ucsb.nceas.metacat.dataone.convert;
|
25
|
|
26
|
import java.io.IOException;
|
27
|
import java.lang.reflect.InvocationTargetException;
|
28
|
import org.dataone.exceptions.MarshallingException;
|
29
|
import org.dataone.service.types.v1.Event;
|
30
|
import org.dataone.service.types.v2.Log;
|
31
|
import org.dataone.service.types.v2.LogEntry;
|
32
|
|
33
|
/**
|
34
|
* This class represents a converter to convert an DataONE v2 Log object to a v1 Log object.
|
35
|
* It probably will be removed to d1_common_java in the future.
|
36
|
* @author tao
|
37
|
*
|
38
|
*/
|
39
|
public class LogV2toV1Converter {
|
40
|
|
41
|
/**
|
42
|
* Default constructor
|
43
|
*/
|
44
|
public LogV2toV1Converter() {
|
45
|
|
46
|
}
|
47
|
|
48
|
/**
|
49
|
* Convert a v2 Log object to a v1 Log object
|
50
|
* @param logV2 - the v2 Log object which needs to be converted
|
51
|
* @return a v1 Log object. If the logV2 is null, null will be returned.
|
52
|
* @throws IOException
|
53
|
* @throws JiBXException
|
54
|
* @throws InvocationTargetException
|
55
|
* @throws IllegalAccessException
|
56
|
* @throws InstantiationException
|
57
|
*/
|
58
|
public org.dataone.service.types.v1.Log convert(Log logV2)
|
59
|
throws InstantiationException, IllegalAccessException, InvocationTargetException, MarshallingException, IOException {
|
60
|
org.dataone.service.types.v1.Log logV1 = null;
|
61
|
int removedLogCount =0;
|
62
|
if(logV2 != null) {
|
63
|
//System.out.println("====================== logV2 is not null and the size is "+logV2.getCount());
|
64
|
LogEntryV2toV1Converter converter = new LogEntryV2toV1Converter();
|
65
|
logV1 = new org.dataone.service.types.v1.Log();
|
66
|
for(int i=0; i<logV2.getCount(); i++) {
|
67
|
LogEntry v2LogEntry = logV2.getLogEntry(i);
|
68
|
org.dataone.service.types.v1.LogEntry v1LogEntry = converter.convert(v2LogEntry);
|
69
|
if(v1LogEntry.getEvent() != null) {
|
70
|
logV1.addLogEntry(v1LogEntry);
|
71
|
} else {
|
72
|
removedLogCount ++;
|
73
|
}
|
74
|
|
75
|
}
|
76
|
logV1.setCount(logV2.getCount()-removedLogCount);
|
77
|
logV1.setStart(logV2.getStart());
|
78
|
logV1.setTotal(logV2.getTotal()-removedLogCount);
|
79
|
}
|
80
|
return logV1;
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* A class to convert a v2 LogEntry object to a v1 LogEntry object
|
85
|
* @author tao
|
86
|
*
|
87
|
*/
|
88
|
public static class LogEntryV2toV1Converter {
|
89
|
/**
|
90
|
* Default constructor
|
91
|
*/
|
92
|
public LogEntryV2toV1Converter(){
|
93
|
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* Convert a v2 LogEntry object to a v1 LogEntry object
|
98
|
* @param logV2 - the v2 Log object which needs to be converted
|
99
|
* @return a v1 Log object. If the logV2 is null, null will be returned.
|
100
|
* @throws IOException
|
101
|
* @throws JiBXException
|
102
|
* @throws InvocationTargetException
|
103
|
* @throws IllegalAccessException
|
104
|
* @throws InstantiationException
|
105
|
*/
|
106
|
public org.dataone.service.types.v1.LogEntry convert(LogEntry logEntryV2)
|
107
|
throws InstantiationException, IllegalAccessException, InvocationTargetException, MarshallingException, IOException {
|
108
|
org.dataone.service.types.v1.LogEntry logEntryV1 = null;;
|
109
|
if(logEntryV2 != null) {
|
110
|
logEntryV1 = new org.dataone.service.types.v1.LogEntry();
|
111
|
|
112
|
logEntryV1.setDateLogged(logEntryV2.getDateLogged());
|
113
|
|
114
|
logEntryV1.setEntryId(logEntryV2.getEntryId());
|
115
|
|
116
|
if(logEntryV2.getEvent() != null) {
|
117
|
logEntryV1.setEvent(Event.convert(logEntryV2.getEvent().toLowerCase()));
|
118
|
}
|
119
|
|
120
|
logEntryV1.setIdentifier(logEntryV2.getIdentifier());
|
121
|
|
122
|
logEntryV1.setIpAddress(logEntryV2.getIpAddress());
|
123
|
|
124
|
logEntryV1.setNodeIdentifier(logEntryV2.getNodeIdentifier());
|
125
|
|
126
|
logEntryV1.setSubject(logEntryV2.getSubject());
|
127
|
|
128
|
logEntryV1.setUserAgent(logEntryV2.getUserAgent());
|
129
|
|
130
|
}
|
131
|
return logEntryV1;
|
132
|
}
|
133
|
}
|
134
|
|
135
|
/**
|
136
|
* A wrapper class to gap the difference in the signature of setEvent between the v2 and v1 LogEntry objects.
|
137
|
* (The v2 LogEntry doesn't have the setEvent(Event) method, so TypeMarshaller can't handle it)
|
138
|
* @author tao
|
139
|
*
|
140
|
*/
|
141
|
public static class V1LogEntryWrapper extends org.dataone.service.types.v1.LogEntry{
|
142
|
|
143
|
public void setEvent(String event) {
|
144
|
if(event != null) {
|
145
|
super.setEvent(Event.convert(event));
|
146
|
}
|
147
|
}
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* A wrapper class to gap the difference in the signature of setEvent between the v2 and v1 LogEntry objects.
|
152
|
* (The v2 LogEntry doesn't have the setEvent(Event) method, so TypeMarshaller can't handle it)
|
153
|
* @author tao
|
154
|
*
|
155
|
*/
|
156
|
public static class V2LogEntryWrapper extends LogEntry{
|
157
|
|
158
|
public void setEvent(Event event) {
|
159
|
if(event != null) {
|
160
|
super.setEvent(event.xmlValue());
|
161
|
}
|
162
|
}
|
163
|
}
|
164
|
|
165
|
}
|