Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A class which creates a instance of an IndexEventLog.
4
 *    Copyright: 2013 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Leinfelder
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.IOException;
26
import java.text.ParseException;
27
import java.text.SimpleDateFormat;
28
import java.util.ArrayList;
29
import java.util.Date;
30
import java.util.List;
31

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

    
37
import edu.ucsb.nceas.metacat.common.index.event.IndexEvent;
38
import edu.ucsb.nceas.metacat.index.DistributedMapsFactory;
39

    
40
/**
41
 * @author leinfelder
42
 *
43
 */
44
public class HazelcastIndexEventLog implements IndexEventLog {
45
    
46
    private static final String LASTPROCESSEDDATEFILENAME = "solr-last-proccessed-date";
47
    private File lastProcessedDateFile = null;
48
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
49
    
50
    /**
51
     * Constructor
52
     * @throws IOException
53
     */
54
    public HazelcastIndexEventLog() throws IOException {
55
        String path = Settings.getConfiguration().getString("solr.homeDir");
56
        if(path == null || path.trim().equals("")) {
57
            path = System.getProperty("user.home");
58
        }
59
        File pathDir = new File(path);
60
        lastProcessedDateFile = new File(pathDir, LASTPROCESSEDDATEFILENAME);
61
        if(!lastProcessedDateFile.exists()) {
62
            lastProcessedDateFile.createNewFile();
63
        }
64
    }
65

    
66
	/* (non-Javadoc)
67
	 * @see edu.ucsb.nceas.metacat.index.event.IndexEventLog#write(edu.ucsb.nceas.metacat.index.event.IndexEvent)
68
	 */
69
	@Override
70
	public void write(IndexEvent event) throws IndexEventLogException {
71
		// write to the map
72
		try {
73
			DistributedMapsFactory.getIndexEventMap().put(event.getIdentifier(), event);
74
		} catch (Exception e) {
75
			throw new IndexEventLogException("Could not write to event map", e);
76
		}
77
	}
78

    
79
	/* (non-Javadoc)
80
	 * @see edu.ucsb.nceas.metacat.index.event.IndexEventLog#remove(java.lang.String)
81
	 */
82
	@Override
83
	public void remove(Identifier identifier) throws IndexEventLogException {
84
		// remove from the map
85
		try {
86
			DistributedMapsFactory.getIndexEventMap().remove(identifier);
87
		} catch (Exception e) {
88
			throw new IndexEventLogException("Could not remove from event map", e);
89
		}
90

    
91
	}
92

    
93
	/* (non-Javadoc)
94
	 * @see edu.ucsb.nceas.metacat.index.event.IndexEventLog#getEvents(int, org.dataone.service.types.v1.Identifier, java.util.Date, java.util.Date)
95
	 */
96
	@Override
97
	public List<IndexEvent> getEvents(Event action, Identifier pid, Date start, Date end) throws IndexEventLogException {
98
		try {
99
			// TODO: query the map using the parameters
100
			return new ArrayList<IndexEvent>(DistributedMapsFactory.getIndexEventMap().values());
101
		} catch (Exception e) {
102
			throw new IndexEventLogException("Could not remove from event map", e);
103
		}
104
	}
105

    
106
	/* (non-Javadoc)
107
	 * @see edu.ucsb.nceas.metacat.index.event.IndexEventLog#getLastProcessDate()
108
	 */
109
	@Override
110
	public Date getLastProcessDate() throws IndexEventLogException {
111
	    Date date = null;
112
        try {
113
            String dateStr = FileUtils.readFileToString(lastProcessedDateFile, "UTF-8");
114
            if(dateStr != null && !dateStr.trim().equals("")) {
115
                date = format.parse(dateStr);
116
            }
117
        } catch (IOException e) {
118
            throw new IndexEventLogException("HazelcastIndexEventLog.getLastProcessedDate - couldn't read the last processed date :", e);
119
        } catch (ParseException e) {
120
            // TODO Auto-generated catch block
121
            throw new IndexEventLogException("HazelcastIndexEventLog.getLastProcessedDate - couldn't read the last processed date since the content of file is not date :", e);
122
        }
123
        return date;
124
	}
125

    
126
	/* (non-Javadoc)
127
	 * @see edu.ucsb.nceas.metacat.index.event.IndexEventLog#setLastProcessDate(java.util.Date)
128
	 */
129
	@Override
130
	public void setLastProcessDate(Date date) throws IndexEventLogException {
131
	    String dateStr = format.format(date);
132
        try {
133
            FileUtils.writeStringToFile(lastProcessedDateFile, dateStr, "UTF-8");
134
        } catch (IOException e) {
135
            // TODO Auto-generated catch block
136
           throw new IndexEventLogException("HazelcastIndexEventLog.setLastProcessedDate - couldn't set the last processed date :", e);
137
        }
138

    
139
	}
140

    
141
}
(2-2/5)