Project

General

Profile

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

    
29
import org.apache.commons.beanutils.BeanUtils;
30
import org.dataone.service.types.v1.Event;
31
import org.dataone.service.types.v2.Log;
32
import org.dataone.service.types.v2.LogEntry;
33
import org.dataone.service.util.TypeMarshaller;
34
import org.jibx.runtime.JiBXException;
35
/**
36
 * This class represents a converter to convert an DataONE v2 Log object to a v1 Log object.
37
 * It probably will be removed to d1_common_java in the future. 
38
 * @author tao
39
 *
40
 */
41
public class LogV2toV1Converter {
42
    
43
    /**
44
     * Default constructor
45
     */
46
    public LogV2toV1Converter() {
47
        
48
    }
49
    
50
    /**
51
     * Convert a v2 Log object to a v1 Log object
52
     * @param logV2  - the v2 Log object which needs to be converted
53
     * @return a v1 Log object. If the logV2 is null, null will be returned.
54
     * @throws IOException 
55
     * @throws JiBXException 
56
     * @throws InvocationTargetException 
57
     * @throws IllegalAccessException 
58
     * @throws InstantiationException 
59
     */
60
    public org.dataone.service.types.v1.Log convert(Log logV2) 
61
            throws InstantiationException, IllegalAccessException, InvocationTargetException, JiBXException, IOException {
62
        org.dataone.service.types.v1.Log logV1 = null;
63
        if(logV2 != null) {
64
            //System.out.println("====================== logV2 is not null and the size is "+logV2.getCount());
65
            LogEntryV2toV1Converter converter = new LogEntryV2toV1Converter();
66
            logV1 = new org.dataone.service.types.v1.Log();
67
            for(int i=0; i<logV2.getCount(); i++) {
68
                LogEntry v2LogEntry = logV2.getLogEntry(i);
69
                org.dataone.service.types.v1.LogEntry v1LogEntry = converter.convert(v2LogEntry);
70
                logV1.addLogEntry(v1LogEntry);
71
            }
72
            logV1.setCount(logV2.getCount());
73
            logV1.setStart(logV2.getStart());
74
            logV1.setTotal(logV2.getTotal());
75
        }
76
        return logV1;
77
    }
78
    
79
    /**
80
     * A class to convert a v2 LogEntry object to a v1 LogEntry object
81
     * @author tao
82
     *
83
     */
84
    public static class LogEntryV2toV1Converter {
85
        /**
86
         * Default constructor
87
         */
88
        public LogEntryV2toV1Converter(){
89
            
90
        }
91
        
92
        /**
93
         * Convert a v2 LogEntry object to a v1 LogEntry object
94
         * @param logV2  - the v2 Log object which needs to be converted
95
         * @return a v1 Log object. If the logV2 is null, null will be returned.
96
         * @throws IOException 
97
         * @throws JiBXException 
98
         * @throws InvocationTargetException 
99
         * @throws IllegalAccessException 
100
         * @throws InstantiationException 
101
         */
102
        public org.dataone.service.types.v1.LogEntry convert(LogEntry logEntryV2) 
103
                throws InstantiationException, IllegalAccessException, InvocationTargetException, JiBXException, IOException {
104
            org.dataone.service.types.v1.LogEntry logEntryV1 = null;;
105
            if(logEntryV2 != null) {
106
                logEntryV1 = new org.dataone.service.types.v1.LogEntry();
107
               
108
                logEntryV1.setDateLogged(logEntryV2.getDateLogged());
109
                
110
                logEntryV1.setEntryId(logEntryV2.getEntryId());
111
              
112
                if(logEntryV2.getEvent() != null) {
113
                    logEntryV1.setEvent(Event.convert(logEntryV2.getEvent().toLowerCase()));
114
                }
115
                
116
                logEntryV1.setIdentifier(logEntryV2.getIdentifier());
117
                
118
                logEntryV1.setIpAddress(logEntryV2.getIpAddress());
119
                
120
                logEntryV1.setNodeIdentifier(logEntryV2.getNodeIdentifier());
121
                
122
                logEntryV1.setSubject(logEntryV2.getSubject());
123
                
124
                logEntryV1.setUserAgent(logEntryV2.getUserAgent());
125
                
126
            }
127
            return logEntryV1;
128
        }
129
    }
130
    
131
    /**
132
     * A wrapper class to gap the difference in the signature of setEvent between the v2 and v1 LogEntry objects.
133
     * (The v2 LogEntry doesn't have the setEvent(Event) method, so TypeMarshaller can't handle it)
134
     * @author tao
135
     *
136
     */
137
    public static class V1LogEntryWrapper extends org.dataone.service.types.v1.LogEntry{
138
        
139
        public void setEvent(String event) { 
140
            if(event != null) {
141
                super.setEvent(Event.convert(event));
142
            }
143
        }
144
    }
145
    
146
    /**
147
     * A wrapper class to gap the difference in the signature of setEvent between the v2 and v1 LogEntry objects.
148
     * (The v2 LogEntry doesn't have the setEvent(Event) method, so TypeMarshaller can't handle it)
149
     * @author tao
150
     *
151
     */
152
    public static class V2LogEntryWrapper extends LogEntry{
153
        
154
        public void setEvent(Event event) { 
155
            if(event != null) {
156
                super.setEvent(event.xmlValue());
157
            }
158
        }
159
    }
160

    
161
}
    (1-1/1)