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.FileOutputStream;
27
import java.io.IOException;
28
import java.text.DateFormat;
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.apache.commons.io.IOUtils;
36
import org.dataone.configuration.Settings;
37
import org.dataone.service.types.v1.Identifier;
38

    
39

    
40
/**
41
 * A class represents a file log for the index events.
42
 * @author tao
43
 *
44
 */
45
public class IndexEventFileLog implements IndexEventLog {
46
    private static final String FIELDSEPERATOR = " ";
47
    private static final String LOGFILENAME = "solr-index.log";
48
    private File logFile = null;
49
    private long index=1;
50
    
51
    /**
52
     * Constructor. Initialize the log file. The log file locates solr.homeDir. 
53
     * If the solr.homeDir doesn't exists in the metacat.properties, the it will locate the user's home directory.
54
     * @throws IOException 
55
     */
56
    public IndexEventFileLog() throws IOException {
57
        String path = Settings.getConfiguration().getString("solr.homeDir");
58
        if(path == null || path.trim().equals("")) {
59
            path = System.getProperty("user.home");
60
        }
61
        File pathDir = new File(path);
62
        if(!pathDir.exists()) {
63
            pathDir.mkdirs();
64
        }
65
        logFile = new File(pathDir, LOGFILENAME);
66
        if(!logFile.exists()) {
67
            logFile.createNewFile();
68
        }
69
    }
70
    /**
71
     * Write an IndexEvent into a file
72
     * @param event
73
     * @return the serial number for this event
74
     * @throws IndexEventLogException
75
     */
76
    public synchronized long write(IndexEvent event) throws IndexEventLogException {
77
        if(event != null) {
78
            Vector<String> lines = new Vector<String>();
79
            StringBuffer lineBuffer = new StringBuffer();
80
            lineBuffer.append("\""+index+"\""+FIELDSEPERATOR);
81
            int type = event.getType();
82
            lineBuffer.append("\""+type+"\""+FIELDSEPERATOR);
83
            Date date = event.getDate();
84
            if(date != null) {
85
                DateFormat formate = new SimpleDateFormat();
86
                lineBuffer.append("\""+formate.format(date)+"\""+FIELDSEPERATOR);
87
            }
88
            Identifier id = event.getPid();
89
            if(id != null) {
90
                lineBuffer.append("\""+id.getValue()+"\""+FIELDSEPERATOR);
91
            }
92
            String description = event.getDescription();
93
            if(description != null) {
94
                lineBuffer.append("\""+description+"\""+FIELDSEPERATOR);
95
            }
96
            lines.add(lineBuffer.toString());
97
            String lineEnding = null;//null means to use the system default one.
98
            boolean append = true;
99
            try {
100
                FileUtils.writeLines(logFile, "UTF-8", lines, append);
101
                //FileUtils.writeLines(logFile, "UTF-8", lines, lineEnding);
102
                //IOUtils.writeLines(lines, lineEnding, new FileOutputStream(logFile), "UTF-8");
103
            } catch (FileNotFoundException e) {
104
                // TODO Auto-generated catch block
105
                throw new IndexEventLogException(e.getMessage());
106
            } catch (IOException e) {
107
                // TODO Auto-generated catch block
108
                throw new IndexEventLogException(e.getMessage());
109
            }
110
            index++;
111
        }
112
        return index;
113
    }
114
    
115
    
116
    /**
117
     * Gets the list of IndexEvent matching the specified set of filters. The filter parameters can be null
118
     * @param type  the type of the event
119
     * @param pid   the identifier of the data object in the event
120
     * @param start the start time of the time range for query
121
     * @param end   the end time of the time range for query
122
     * @return
123
     * @throws IndexEventLogException
124
     */
125
    public List<IndexEvent> getEvents(int type, Identifier pid, boolean archvied, Date start, Date end) throws IndexEventLogException {
126
        List<IndexEvent> list = null;
127
        return list;
128
    }
129
    
130
    /**
131
     * Set the event with the specified serial number to be archived
132
     * @param serialNumber
133
     */
134
    public void setArchived(long serialNumber) throws IndexEventLogException {
135
        
136
    }
137
    
138
    /**
139
     * Set the all events with the specified identifier to be archived
140
     * @param pid
141
     */
142
    public void setArchived(Identifier pid) throws IndexEventLogException {
143
        
144
    }
145
}
(3-3/5)