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
        int removedLogCount =0;
64
        if(logV2 != null) {
65
            //System.out.println("====================== logV2 is not null and the size is "+logV2.getCount());
66
            LogEntryV2toV1Converter converter = new LogEntryV2toV1Converter();
67
            logV1 = new org.dataone.service.types.v1.Log();
68
            for(int i=0; i<logV2.getCount(); i++) {
69
                LogEntry v2LogEntry = logV2.getLogEntry(i);
70
                org.dataone.service.types.v1.LogEntry v1LogEntry = converter.convert(v2LogEntry);
71
                if(v1LogEntry.getEvent() != null) {
72
                    logV1.addLogEntry(v1LogEntry);
73
                } else {
74
                    removedLogCount ++;
75
                }
76
                
77
            }
78
            logV1.setCount(logV2.getCount()-removedLogCount);
79
            logV1.setStart(logV2.getStart());
80
            logV1.setTotal(logV2.getTotal()-removedLogCount);
81
        }
82
        return logV1;
83
    }
84
    
85
    /**
86
     * A class to convert a v2 LogEntry object to a v1 LogEntry object
87
     * @author tao
88
     *
89
     */
90
    public static class LogEntryV2toV1Converter {
91
        /**
92
         * Default constructor
93
         */
94
        public LogEntryV2toV1Converter(){
95
            
96
        }
97
        
98
        /**
99
         * Convert a v2 LogEntry object to a v1 LogEntry object
100
         * @param logV2  - the v2 Log object which needs to be converted
101
         * @return a v1 Log object. If the logV2 is null, null will be returned.
102
         * @throws IOException 
103
         * @throws JiBXException 
104
         * @throws InvocationTargetException 
105
         * @throws IllegalAccessException 
106
         * @throws InstantiationException 
107
         */
108
        public org.dataone.service.types.v1.LogEntry convert(LogEntry logEntryV2) 
109
                throws InstantiationException, IllegalAccessException, InvocationTargetException, JiBXException, IOException {
110
            org.dataone.service.types.v1.LogEntry logEntryV1 = null;;
111
            if(logEntryV2 != null) {
112
                logEntryV1 = new org.dataone.service.types.v1.LogEntry();
113
               
114
                logEntryV1.setDateLogged(logEntryV2.getDateLogged());
115
                
116
                logEntryV1.setEntryId(logEntryV2.getEntryId());
117
              
118
                if(logEntryV2.getEvent() != null) {
119
                    logEntryV1.setEvent(Event.convert(logEntryV2.getEvent().toLowerCase()));
120
                }
121
                
122
                logEntryV1.setIdentifier(logEntryV2.getIdentifier());
123
                
124
                logEntryV1.setIpAddress(logEntryV2.getIpAddress());
125
                
126
                logEntryV1.setNodeIdentifier(logEntryV2.getNodeIdentifier());
127
                
128
                logEntryV1.setSubject(logEntryV2.getSubject());
129
                
130
                logEntryV1.setUserAgent(logEntryV2.getUserAgent());
131
                
132
            }
133
            return logEntryV1;
134
        }
135
    }
136
    
137
    /**
138
     * A wrapper class to gap the difference in the signature of setEvent between the v2 and v1 LogEntry objects.
139
     * (The v2 LogEntry doesn't have the setEvent(Event) method, so TypeMarshaller can't handle it)
140
     * @author tao
141
     *
142
     */
143
    public static class V1LogEntryWrapper extends org.dataone.service.types.v1.LogEntry{
144
        
145
        public void setEvent(String event) { 
146
            if(event != null) {
147
                super.setEvent(Event.convert(event));
148
            }
149
        }
150
    }
151
    
152
    /**
153
     * A wrapper class to gap the difference in the signature of setEvent between the v2 and v1 LogEntry objects.
154
     * (The v2 LogEntry doesn't have the setEvent(Event) method, so TypeMarshaller can't handle it)
155
     * @author tao
156
     *
157
     */
158
    public static class V2LogEntryWrapper extends LogEntry{
159
        
160
        public void setEvent(Event event) { 
161
            if(event != null) {
162
                super.setEvent(event.xmlValue());
163
            }
164
        }
165
    }
166

    
167
}
    (1-1/1)