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.Identifier;
37

    
38

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