Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A class represents a file log for the index events.
4
 *    Copyright: 2013 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Jing Tao
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 2 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
 */
22
package edu.ucsb.nceas.metacat.index.event;
23

    
24
import java.io.File;
25
import java.io.FileNotFoundException;
26
import java.io.IOException;
27
import java.text.DateFormat;
28
import java.text.ParseException;
29
import java.text.SimpleDateFormat;
30
import java.util.Date;
31
import java.util.List;
32
import java.util.Vector;
33

    
34
import org.apache.commons.io.FileUtils;
35
import org.dataone.configuration.Settings;
36
import org.dataone.service.types.v1.Event;
37
import org.dataone.service.types.v1.Identifier;
38

    
39
import edu.ucsb.nceas.metacat.common.index.event.IndexEvent;
40

    
41

    
42
/**
43
 * A class represents a file log for the index events.
44
 * @author tao
45
 *
46
 */
47
public class IndexEventFileLog implements IndexEventLog {
48
    private static final String FIELDSEPERATOR = " ";
49
    private static final String LOGFILENAME = "solr-index.log";
50
    private static final String LASTPROCESSEDDATEFILENAME = "solr-last-proccessed-date";
51
    private File logFile = null;
52
    private File lastProcessedDateFile = null;
53
    private long index=1;
54
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
55
    
56
    /**
57
     * Constructor. Initialize the log file. The log file locates solr.homeDir. 
58
     * If the solr.homeDir doesn't exists in the metacat.properties, the it will locate the user's home directory.
59
     * @throws IOException 
60
     */
61
    public IndexEventFileLog() throws IOException {
62
        String path = Settings.getConfiguration().getString("solr.homeDir");
63
        if(path == null || path.trim().equals("")) {
64
            path = System.getProperty("user.home");
65
        }
66
        File pathDir = new File(path);
67
        if(!pathDir.exists()) {
68
            pathDir.mkdirs();
69
        }
70
        logFile = new File(pathDir, LOGFILENAME);
71
        if(!logFile.exists()) {
72
            logFile.createNewFile();
73
        }
74
        lastProcessedDateFile = new File(pathDir, LASTPROCESSEDDATEFILENAME);
75
        if(!lastProcessedDateFile.exists()) {
76
            lastProcessedDateFile.createNewFile();
77
        }
78
    }
79
    /**
80
     * Write an IndexEvent into a file
81
     * @param event
82
     * @throws IndexEventLogException
83
     */
84
    public synchronized void write(IndexEvent event) throws IndexEventLogException {
85
        if(event != null) {
86
            Vector<String> lines = new Vector<String>();
87
            StringBuffer lineBuffer = new StringBuffer();
88
            lineBuffer.append("\""+index+"\""+FIELDSEPERATOR);
89
            Event type = event.getAction();
90
            lineBuffer.append("\"" + type.xmlValue() + "\"" + FIELDSEPERATOR);
91
            Date date = event.getDate();
92
            if(date != null) {
93
                DateFormat formate = new SimpleDateFormat();
94
                lineBuffer.append("\""+formate.format(date)+"\""+FIELDSEPERATOR);
95
            }
96
            Identifier id = event.getIdentifier();
97
            if(id != null) {
98
                lineBuffer.append("\""+id.getValue()+"\""+FIELDSEPERATOR);
99
            }
100
            String description = event.getDescription();
101
            if(description != null) {
102
                lineBuffer.append("\""+description+"\""+FIELDSEPERATOR);
103
            }
104
            lines.add(lineBuffer.toString());
105
            String lineEnding = null;//null means to use the system default one.
106
            boolean append = true;
107
            try {
108
                FileUtils.writeLines(logFile, "UTF-8", lines, append);
109
                //FileUtils.writeLines(logFile, "UTF-8", lines, lineEnding);
110
                //IOUtils.writeLines(lines, lineEnding, new FileOutputStream(logFile), "UTF-8");
111
            } catch (FileNotFoundException e) {
112
                // TODO Auto-generated catch block
113
                throw new IndexEventLogException(e.getMessage());
114
            } catch (IOException e) {
115
                // TODO Auto-generated catch block
116
                throw new IndexEventLogException(e.getMessage());
117
            }
118
            index++;
119
        }
120
        return;
121
    }
122
    
123
    
124
    /**
125
     * Gets the list of IndexEvent matching the specified set of filters. The filter parameters can be null
126
     * @param action  the action of the event
127
     * @param pid   the identifier of the data object in the event
128
     * @param start the start time of the time range for query
129
     * @param end   the end time of the time range for query
130
     * @return
131
     * @throws IndexEventLogException
132
     */
133
    public List<IndexEvent> getEvents(Event action, Identifier pid, Date start, Date end) throws IndexEventLogException {
134
        List<IndexEvent> list = null;
135
        return list;
136
    }
137
    
138
    
139
    /**
140
     * Get the latest SystemMetadata modification Date of the objects that were built
141
     * the solr index during the previous timed indexing process.
142
     * @return the date. The null will be returned if there is no such date.
143
     * @throws IndexEventLogException
144
     */
145
    public Date getLastProcessDate() throws IndexEventLogException {
146
        Date date = null;
147
        try {
148
            String dateStr = FileUtils.readFileToString(lastProcessedDateFile, "UTF-8");
149
            if(dateStr != null && !dateStr.trim().equals("")) {
150
                date = format.parse(dateStr);
151
            }
152
        } catch (IOException e) {
153
            throw new IndexEventLogException("IndexEventFileLog.getLastProcessedDate - couldn't read the last processed date :", e);
154
        } catch (ParseException e) {
155
            // TODO Auto-generated catch block
156
            throw new IndexEventLogException("IndexEventFileLog.getLastProcessedDate - couldn't read the last processed date since the content of file is not date :", e);
157
        }
158
        return date;
159
    }
160
    
161
    
162
    /**
163
     * Set the SystemMetadata modification Date of the objects that were built
164
     * the solr index during the previous timed indexing process.
165
     * @throws IndexEventLogException
166
     */
167
    public void setLastProcessDate(Date date) throws IndexEventLogException {
168
        String dateStr = format.format(date);
169
        try {
170
            FileUtils.writeStringToFile(lastProcessedDateFile, dateStr, "UTF-8");
171
        } catch (IOException e) {
172
            // TODO Auto-generated catch block
173
           throw new IndexEventLogException("IndexEventFileLog.setLastProcessedDate - couldn't set the last processed date :", e);
174
        }
175
    }
176
    
177
	@Override
178
	public void remove(Identifier identifier) throws IndexEventLogException {
179
		// TODO Auto-generated method stub
180
		
181
	}
182
}
(3-3/5)