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

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

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

    
92
	}
93

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

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

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

    
140
	}
141

    
142
}
(2-2/5)