Project

General

Profile

1 7606 tao
/**
2 8138 tao
 *  Copyright: 2013 Regents of the University of California and the
3 7606 tao
 *             National Center for Ecological Analysis and Synthesis
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
package edu.ucsb.nceas.metacat.index;
20
21 7924 tao
import java.io.File;
22 7691 tao
import java.io.FileInputStream;
23
import java.io.FileNotFoundException;
24 7876 tao
import java.io.IOException;
25 7606 tao
import java.io.InputStream;
26 7924 tao
import java.text.SimpleDateFormat;
27 7606 tao
import java.util.ArrayList;
28 7857 tao
import java.util.Calendar;
29 7876 tao
import java.util.Collections;
30 7606 tao
import java.util.Date;
31
import java.util.List;
32 7788 tao
import java.util.TimerTask;
33 7606 tao
34 7876 tao
import javax.xml.parsers.ParserConfigurationException;
35
import javax.xml.xpath.XPathExpressionException;
36
37 7924 tao
import org.apache.commons.io.FileUtils;
38 7606 tao
import org.apache.commons.logging.Log;
39
import org.apache.commons.logging.LogFactory;
40
import org.apache.solr.client.solrj.SolrServerException;
41
import org.dataone.configuration.Settings;
42
import org.dataone.service.exceptions.InvalidRequest;
43
import org.dataone.service.exceptions.InvalidToken;
44
import org.dataone.service.exceptions.NotAuthorized;
45 7876 tao
import org.dataone.service.exceptions.NotFound;
46 7606 tao
import org.dataone.service.exceptions.NotImplemented;
47
import org.dataone.service.exceptions.ServiceFailure;
48 7876 tao
import org.dataone.service.exceptions.UnsupportedType;
49 7857 tao
import org.dataone.service.types.v1.Event;
50 7606 tao
import org.dataone.service.types.v1.Identifier;
51 7740 tao
import org.dataone.service.types.v1.ObjectFormatIdentifier;
52 7606 tao
import org.dataone.service.types.v1.SystemMetadata;
53 8023 tao
import org.dspace.foresite.OREParserException;
54 7876 tao
import org.xml.sax.SAXException;
55 7606 tao
56 7691 tao
import com.hazelcast.core.IMap;
57 7793 tao
import com.hazelcast.core.ISet;
58 7606 tao
59 7924 tao
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
60 8464 leinfelder
import edu.ucsb.nceas.metacat.common.index.IndexTask;
61 7828 leinfelder
import edu.ucsb.nceas.metacat.common.index.event.IndexEvent;
62 7802 tao
import edu.ucsb.nceas.metacat.index.event.EventlogFactory;
63 7806 tao
import edu.ucsb.nceas.metacat.index.event.IndexEventLogException;
64 7691 tao
65 7802 tao
66 7606 tao
/**
67
 * A class represents the object to generate massive solr indexes.
68
 * This can happen during an update of Metacat (generating index for all existing documents)
69
 * or regenerate index for those documents
70
 * failing to build index during the insert or update.
71
 *
72
 * @author tao
73
 *
74
 */
75 8352 tao
public class IndexGeneratorTimerTask extends TimerTask {
76 7606 tao
77 7740 tao
    private static final int FIRST =0;
78
    private static final int SECOND =1;
79 7876 tao
    private static final int THIRD = 2;
80
    private static final int FOURTH = 3;
81 7774 tao
    public static final int WAITTIME = 10000;
82
    public static final int MAXWAITNUMBER = 180;
83 7606 tao
    private static final String HTTP = "http://";
84
    private static final String MNAPPENDIX = "/d1/mn";
85 7740 tao
    private static final String RESOURCEMAPPROPERYNAME = "index.resourcemap.namespace";
86 7774 tao
    public static final String WAITIMEPOPERTYNAME = "index.regenerate.start.waitingtime";
87
    public static final String MAXATTEMPTSPROPERTYNAME = "index.regenerate.start.maxattempts";
88 7748 tao
89
90 7606 tao
    private SolrIndex solrIndex = null;
91 7691 tao
    //private SystemMetadataEventListener systemMetadataListener = null;
92
    private IMap<Identifier, SystemMetadata> systemMetadataMap;
93
    private IMap<Identifier, String> objectPathMap;
94 8464 leinfelder
    private IMap<Identifier, IndexTask> indexQueue;
95 8352 tao
    private Log log = LogFactory.getLog(IndexGeneratorTimerTask.class);
96 7793 tao
    //private MNode mNode = null;
97 7786 tao
    private static List<String> resourceMapNamespaces = null;
98 7606 tao
99
    /**
100
     * Constructor
101
     * @param solrIndex
102
     * @param systemMetadataListener
103
     */
104 8352 tao
    public IndexGeneratorTimerTask(SolrIndex solrIndex) {
105 7606 tao
        this.solrIndex = solrIndex;
106 7740 tao
        resourceMapNamespaces = Settings.getConfiguration().getList(RESOURCEMAPPROPERYNAME);
107 7691 tao
        //this.systemMetadataListener = systemMetadataListener;
108 7793 tao
        //this.mNode = new MNode(buildMNBaseURL());
109 8292 tao
110 7606 tao
    }
111
112 8343 tao
113 7606 tao
114
    /**
115 7806 tao
     * Build the index for all documents.
116 7606 tao
     * @throws SolrServerException
117
     * @throws ServiceFailure
118
     * @throws NotImplemented
119
     * @throws NotAuthorized
120
     * @throws InvalidToken
121
     * @throws InvalidRequest
122 7806 tao
     * @throws IndexEventLogException
123
     * @throws IllegalAccessException
124
     * @throws InstantiationException
125
     * @throws ClassNotFoundException
126 7876 tao
     * @throws ParserConfigurationException
127
     * @throws SAXException
128
     * @throws IOException
129
     * @throws UnsupportedType
130
     * @throws NotFound
131
     * @throws XPathExpressionException
132 8023 tao
     * @throws OREParserException
133 7606 tao
     */
134 7806 tao
    public void indexAll() throws InvalidRequest, InvalidToken,
135 8023 tao
                NotAuthorized, NotImplemented, ServiceFailure, SolrServerException, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException, XPathExpressionException, NotFound, UnsupportedType, IOException, SAXException, ParserConfigurationException, OREParserException {
136 7606 tao
        Date since = null;
137
        Date until = null;
138 7806 tao
        index(since, until);
139 7606 tao
    }
140
141
    /**
142
     * Build the index for the docs which have been modified since the specified date.
143
     * @param since
144
     * @throws SolrServerException
145
     * @throws ServiceFailure
146
     * @throws NotImplemented
147
     * @throws NotAuthorized
148
     * @throws InvalidToken
149
     * @throws InvalidRequest
150 7806 tao
     * @throws IndexEventLogException
151
     * @throws IllegalAccessException
152
     * @throws InstantiationException
153
     * @throws ClassNotFoundException
154 7876 tao
     * @throws ParserConfigurationException
155
     * @throws SAXException
156
     * @throws IOException
157
     * @throws UnsupportedType
158
     * @throws NotFound
159
     * @throws XPathExpressionException
160 8023 tao
     * @throws OREParserException
161 7606 tao
     */
162 7806 tao
    public void index(Date since) throws InvalidRequest, InvalidToken,
163 8023 tao
                    NotAuthorized, NotImplemented, ServiceFailure, SolrServerException, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException, XPathExpressionException, NotFound, UnsupportedType, IOException, SAXException, ParserConfigurationException, OREParserException {
164 7606 tao
        Date until = null;
165 7806 tao
        index(since, until);
166 7606 tao
    }
167
168
    /**
169
     *  Build the index for the docs which have been modified between the specified date.s
170
     * @param since
171
     * @param until
172
     * @throws SolrServerException
173
     * @throws ServiceFailure
174
     * @throws NotImplemented
175
     * @throws NotAuthorized
176
     * @throws InvalidToken
177
     * @throws InvalidRequest
178 7806 tao
     * @throws IndexEventLogException
179
     * @throws IllegalAccessException
180
     * @throws InstantiationException
181
     * @throws ClassNotFoundException
182 7876 tao
     * @throws ParserConfigurationException
183
     * @throws SAXException
184
     * @throws IOException
185
     * @throws UnsupportedType
186
     * @throws NotFound
187
     * @throws XPathExpressionException
188 8023 tao
     * @throws OREParserException
189 7606 tao
     */
190 7806 tao
    public void index(Date since, Date until) throws SolrServerException, InvalidRequest,
191 8023 tao
                                                InvalidToken, NotAuthorized, NotImplemented, ServiceFailure, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException, XPathExpressionException, NotFound, UnsupportedType, IOException, SAXException, ParserConfigurationException, OREParserException {
192 7806 tao
        Date processedDate = null;
193 7606 tao
        List<String> solrIds = null;
194 7740 tao
        initSystemMetadataMap();
195
        initObjectPathMap();
196 8293 tao
        initIndexQueue();
197 7806 tao
        List[] metacatIds = getMetacatIds(since, until);
198
        List<String> otherMetacatIds = metacatIds[FIRST];
199
        List<String> resourceMapIds =  metacatIds[SECOND];
200 8291 tao
        //List<String> otherDeletedMetacatIds = metacatIds[THIRD];
201
        //List<String> resourceMapDeletedIds = metacatIds[FOURTH];
202 7806 tao
203
        //figure out the procesedDate by comparing the last element of otherMetacatIds and resourceMapIds.
204 7876 tao
        List<Long> maxCollection = new ArrayList<Long>();
205 7806 tao
        Date latestOtherId = null;
206
        if (otherMetacatIds != null && !otherMetacatIds.isEmpty()) {
207
            int size = otherMetacatIds.size();
208
            String id = otherMetacatIds.get(size-1);
209
            SystemMetadata sysmeta = getSystemMetadata(id);
210
            latestOtherId = sysmeta.getDateSysMetadataModified();
211 7876 tao
            maxCollection.add(new Long(latestOtherId.getTime()));
212 7606 tao
        }
213 7876 tao
214 8291 tao
        /*Date latestDeletedOtherIds = null;
215 7876 tao
        if (otherDeletedMetacatIds != null && !otherDeletedMetacatIds.isEmpty()) {
216
            int size = otherDeletedMetacatIds.size();
217
            String id = otherDeletedMetacatIds.get(size-1);
218
            SystemMetadata sysmeta = getSystemMetadata(id);
219
            latestDeletedOtherIds = sysmeta.getDateSysMetadataModified();
220
            maxCollection.add(new Long(latestDeletedOtherIds.getTime()));
221 8291 tao
        }*/
222 7876 tao
223 7806 tao
        Date latestResourceId = null;
224
        if (resourceMapIds != null && !resourceMapIds.isEmpty()) {
225
            int size = resourceMapIds.size();
226
            String id = resourceMapIds.get(size-1);
227
            SystemMetadata sysmeta = getSystemMetadata(id);
228
            latestResourceId = sysmeta.getDateSysMetadataModified();
229 7876 tao
            maxCollection.add(new Long(latestResourceId.getTime()));
230 7806 tao
        }
231 7876 tao
232 8291 tao
        /*Date latestDeletedResourceId = null;
233 7876 tao
        if(resourceMapDeletedIds != null && !resourceMapDeletedIds.isEmpty()) {
234
            int size = resourceMapDeletedIds.size();
235
            String id = resourceMapDeletedIds.get(size-1);
236
            SystemMetadata sysmeta = getSystemMetadata(id);
237
            latestDeletedResourceId = sysmeta.getDateSysMetadataModified();
238
            maxCollection.add(new Long(latestDeletedResourceId.getTime()));
239 8291 tao
        }*/
240 7876 tao
241
        if(!maxCollection.isEmpty()) {
242
            Long max = Collections.max(maxCollection);
243
            processedDate = new Date(max.longValue());
244
        }
245
        /*if(latestOtherId != null && latestResourceId != null && latestOtherId.getTime() > latestResourceId.getTime()) {
246 7806 tao
            processedDate = latestOtherId;
247
        } else if (latestOtherId != null && latestResourceId != null && latestOtherId.getTime()  <= latestResourceId.getTime()) {
248
            processedDate = latestResourceId;
249
        } else if (latestOtherId == null && latestResourceId != null) {
250
            processedDate = latestResourceId;
251
        } else if (latestOtherId != null && latestResourceId == null) {
252
            processedDate = latestOtherId;
253 7876 tao
        }*/
254 7806 tao
255 7876 tao
256 7806 tao
        //add the failedPids
257 7815 leinfelder
        List<IndexEvent> failedEvents = EventlogFactory.createIndexEventLog().getEvents(null, null, null, null);
258 8293 tao
        List<String> failedOtherIds = new ArrayList<String>();
259
        List<String> failedResourceMapIds = new ArrayList<String>();
260 7815 leinfelder
        if(failedEvents != null) {
261
            for(IndexEvent event : failedEvents) {
262
            	String id = event.getIdentifier().getValue();
263 7806 tao
                SystemMetadata sysmeta = getSystemMetadata(id);
264 7857 tao
                if(sysmeta != null) {
265 7806 tao
                    ObjectFormatIdentifier formatId =sysmeta.getFormatId();
266
                    if(formatId != null && formatId.getValue() != null && resourceMapNamespaces != null && isResourceMap(formatId)) {
267 8293 tao
                        failedResourceMapIds.add(id);
268 7806 tao
                    } else {
269 8293 tao
                        failedOtherIds.add(id);
270 7806 tao
                    }
271
                }
272
            }
273
        }
274 8293 tao
        //indexFailedIds(failedOtherIds);
275
        //indexFailedIds(failedResourceMapIds);
276 7806 tao
277 8293 tao
        index(failedOtherIds);
278
        index(failedResourceMapIds);
279
280 7857 tao
        /*if(!failedOtherIds.isEmpty()) {
281 7806 tao
            failedOtherIds.addAll(otherMetacatIds);
282
        } else {
283
            failedOtherIds = otherMetacatIds;
284
        }
285
286
        if(!failedResourceMapIds.isEmpty()) {
287
            failedResourceMapIds.addAll(resourceMapIds);
288
        } else {
289
            failedResourceMapIds = resourceMapIds;
290 7857 tao
        }*/
291 8293 tao
        //log.info("the ids in index_event for reindex ( except the resourcemap)=====================================\n "+failedOtherIds);
292
        //log.info("the resourcemap ids in index_event for reindex =====================================\n "+failedResourceMapIds);
293 7857 tao
        log.info("the metacat ids (except the resource map ids)-----------------------------"+otherMetacatIds);
294 8027 tao
        //logFile(otherMetacatIds, "ids-for-timed-indexing-log");
295 8291 tao
        //log.info("the deleted metacat ids (except the resource map ids)-----------------------------"+otherDeletedMetacatIds);
296 7857 tao
        log.info("the metacat resroucemap ids -----------------------------"+resourceMapIds);
297 8027 tao
        //logFile(resourceMapIds, "ids-for-timed-indexing-log");
298 8291 tao
        //log.info("the deleted metacat resroucemap ids -----------------------------"+resourceMapDeletedIds);
299 7857 tao
        index(otherMetacatIds);
300 8291 tao
        //removeIndex(otherDeletedMetacatIds);
301 7857 tao
        index(resourceMapIds);
302 8291 tao
        //removeIndex(resourceMapDeletedIds);
303 7740 tao
304 7806 tao
        //record the timed index.
305
        if(processedDate != null) {
306
            EventlogFactory.createIndexEventLog().setLastProcessDate(processedDate);
307
        }
308 7734 tao
309 7740 tao
    }
310
311
    /*
312 7924 tao
     * Write the docids which will be indexed into a file.
313
     */
314 8084 tao
    /*private void logFile(List<String> ids, String fileName)  {
315 7924 tao
        if(ids != null) {
316 7931 tao
            try {
317
                String tempDir = System.getProperty("java.io.tmpdir");
318
                log.info("the temp dir is ===================== "+tempDir);
319 8017 tao
                File idsForIndex = new File(tempDir, fileName);
320 7931 tao
                if(!idsForIndex.exists()) {
321
                    idsForIndex.createNewFile();
322 8017 tao
                }
323
324 7931 tao
                Date date = Calendar.getInstance().getTime();
325
                SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
326
                String dateStr = format.format(date);
327
                List<String> dateList = new ArrayList<String>();
328
                dateList.add(dateStr);
329
                Boolean append = true;
330
                FileUtils.writeLines(idsForIndex, dateList, append);//write time string
331
                FileUtils.writeLines(idsForIndex, ids, append);
332
            } catch (Exception e) {
333
                log.warn("IndexGenerator.logFile - Couldn't log the ids which will be indexed since - "+e.getMessage());
334 7924 tao
            }
335 7931 tao
336 7924 tao
        }
337 8084 tao
    }*/
338 7924 tao
    /*
339 7806 tao
     * Doing index
340 7740 tao
     */
341 7806 tao
    private void index(List<String> metacatIds) {
342 7688 tao
        if(metacatIds != null) {
343 7606 tao
            for(String metacatId : metacatIds) {
344
                if(metacatId != null) {
345 8293 tao
                     generateIndex(metacatId);
346 7606 tao
                }
347
            }
348 7688 tao
        }
349 7606 tao
    }
350
351 7857 tao
    /*
352
     * Index those ids which failed in the process (We got them from the EventLog)
353
     */
354 8293 tao
    /*private void indexFailedIds(List<IndexEvent> events) {
355 7857 tao
        if(events != null) {
356
            for(IndexEvent event : events) {
357
                if(event != null) {
358
                    Identifier identifier = event.getIdentifier();
359
                    if(identifier != null) {
360
                        String id = identifier.getValue();
361
                        if(id != null) {
362
                            Event action = event.getAction();
363 8291 tao
                            //if (action != null && action.equals(Event.CREATE)) {
364 7857 tao
                                try {
365
                                    generateIndex(id);
366
                                    EventlogFactory.createIndexEventLog().remove(identifier);
367
                                } catch (Exception e) {
368
                                    log.error("IndexGenerator.indexFailedIds - Metacat Index couldn't generate the index for the id - "+id+" because "+e.getMessage());
369
                                }
370 8293 tao
371 7857 tao
                        }
372
                    }
373
                }
374
            }
375
        }
376 8293 tao
    }*/
377 7857 tao
378 7613 tao
    public void run() {
379 8343 tao
380 7613 tao
        try {
381 7806 tao
            Date since = EventlogFactory.createIndexEventLog().getLastProcessDate();
382
            index(since);
383 7613 tao
        } catch (InvalidRequest e) {
384
            // TODO Auto-generated catch block
385
            //e.printStackTrace();
386
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
387
        } catch (InvalidToken e) {
388
            // TODO Auto-generated catch block
389
            //e.printStackTrace();
390
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
391
        } catch (NotAuthorized e) {
392
            // TODO Auto-generated catch block
393
            //e.printStackTrace();
394
        } catch (NotImplemented e) {
395
            // TODO Auto-generated catch block
396
            //e.printStackTrace();
397
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
398
        } catch (ServiceFailure e) {
399
            // TODO Auto-generated catch block
400
            //e.printStackTrace();
401
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
402
        } catch (SolrServerException e) {
403
            // TODO Auto-generated catch block
404
            //e.printStackTrace();
405
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
406 7691 tao
        } catch (FileNotFoundException e) {
407
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
408 8343 tao
        } catch (ClassNotFoundException e) {
409 7806 tao
            // TODO Auto-generated catch block
410
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
411
        } catch (InstantiationException e) {
412
            // TODO Auto-generated catch block
413
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
414
        } catch (IllegalAccessException e) {
415
            // TODO Auto-generated catch block
416
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
417
        } catch (IndexEventLogException e) {
418
            // TODO Auto-generated catch block
419
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
420 7876 tao
        } catch (XPathExpressionException e) {
421
            // TODO Auto-generated catch block
422
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
423
        } catch (NotFound e) {
424
            // TODO Auto-generated catch block
425
            e.printStackTrace();
426
        } catch (UnsupportedType e) {
427
            // TODO Auto-generated catch block
428
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
429
        } catch (IOException e) {
430
            // TODO Auto-generated catch block
431
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
432
        } catch (SAXException e) {
433
            // TODO Auto-generated catch block
434
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
435
        } catch (ParserConfigurationException e) {
436
            // TODO Auto-generated catch block
437
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
438 8023 tao
        } catch (OREParserException e) {
439
            // TODO Auto-generated catch block
440
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
441 7806 tao
        }
442 7613 tao
    }
443
444 8293 tao
445 7606 tao
446
    /*
447 7806 tao
     * Get an array of the list of ids of the metacat which has the systemmetadata modification in the range.
448
     *
449
     * If since and util are null, it will return all of them.
450 7740 tao
     * The first element of the list is the ids except the resource map. The second elements of the list is the ids of the resource map.
451 7806 tao
     * The reason to split them is when we index the resource map, we need the index of the documents in the resource map ready.
452
     * The last element in the each list has the latest SystemMetadata modification date. But they are not sorted.
453 7606 tao
     */
454 7793 tao
    private List[] getMetacatIds(Date since, Date until) throws InvalidRequest,
455
                        InvalidToken, NotAuthorized, NotImplemented, ServiceFailure, FileNotFoundException {
456 8017 tao
        String fileName = "ids-from-hazelcast";
457 7740 tao
        List<String> resourceMapIds = new ArrayList();
458 8291 tao
        //List<String> resourceMapDeletedIds = new ArrayList();
459 7740 tao
        List<String> otherIds = new ArrayList();
460 8291 tao
        //List<String> otherDeletedIds = new ArrayList();
461
        List[] ids = new List[2];
462 7740 tao
        ids[FIRST]= otherIds;
463
        ids[SECOND] = resourceMapIds;
464 8291 tao
        //ids[THIRD]  = otherDeletedIds;
465
        //ids[FOURTH] = resourceMapDeletedIds;
466 7793 tao
        ISet<Identifier> metacatIds = DistributedMapsFactory.getIdentifiersSet();
467 7806 tao
        Date otherPreviousDate = null;
468 7876 tao
        Date otherDeletedPreviousDate = null;
469 7806 tao
        Date resourceMapPreviousDate = null;
470 7876 tao
        Date resourceMapDeletedPreviousDate = null;
471 7793 tao
        if(metacatIds != null) {
472
            for(Identifier identifier : metacatIds) {
473
                if(identifier != null && identifier.getValue() != null && !identifier.getValue().equals("")) {
474 8017 tao
                    List<String> idLog = new ArrayList<String>();
475
                    idLog.add(identifier.getValue());
476 8027 tao
                    //logFile(idLog, fileName);
477 7793 tao
                    SystemMetadata sysmeta = getSystemMetadata(identifier.getValue());
478 7876 tao
                    if(sysmeta != null) {
479 7793 tao
                        ObjectFormatIdentifier formatId =sysmeta.getFormatId();
480
                        //System.out.println("the object format id is "+formatId.getValue());
481
                        //System.out.println("the ============ resourcMapNamespaces"+resourceMapNamespaces);
482
                        boolean correctTimeRange = false;
483
                        Date sysDate = sysmeta.getDateSysMetadataModified();
484
                        if(since == null && until == null) {
485
                            correctTimeRange = true;
486
                        } else if (since != null && until == null) {
487 7858 tao
                            if(sysDate.getTime() > since.getTime()) {
488 7793 tao
                                correctTimeRange = true;
489
                            }
490
                        } else if (since == null && until != null) {
491 7858 tao
                            if(sysDate.getTime() < until.getTime()) {
492 7793 tao
                                correctTimeRange = true;
493
                            }
494
                        } else if (since != null && until != null) {
495 7858 tao
                            if(sysDate.getTime() > since.getTime() && sysDate.getTime() < until.getTime()) {
496 7793 tao
                                correctTimeRange = true;
497
                            }
498
                        }
499
                        if(correctTimeRange && formatId != null && formatId.getValue() != null && resourceMapNamespaces != null && isResourceMap(formatId)) {
500 7806 tao
                            //for the resource map
501 8291 tao
                            /*if(sysmeta.getArchived() || sysmeta.getObsoletedBy() != null) {
502 7876 tao
                                //archived ids
503
                                if(!resourceMapDeletedIds.isEmpty()) {
504
                                    if(sysDate.getTime() > resourceMapDeletedPreviousDate.getTime()) {
505
                                        resourceMapDeletedIds.add(identifier.getValue());//append to the end of the list if current is later than the previous one
506
                                        resourceMapDeletedPreviousDate = sysDate;//reset resourceMapPreviousDate to the bigger one
507
                                    } else {
508
                                        int size = resourceMapDeletedIds.size();//
509
                                        resourceMapDeletedIds.add(size -1, identifier.getValue());//keep the previous one at the end of the list.
510
                                    }
511 7806 tao
                                } else {
512 7876 tao
                                    resourceMapDeletedIds.add(identifier.getValue());
513
                                    resourceMapDeletedPreviousDate = sysDate;//init resourcemapPreviousDate
514 7806 tao
                                }
515 8291 tao
                            } else {*/
516
                                // for all ids
517 7876 tao
                                if(!resourceMapIds.isEmpty()) {
518
                                    if(sysDate.getTime() > resourceMapPreviousDate.getTime()) {
519
                                        resourceMapIds.add(identifier.getValue());//append to the end of the list if current is later than the previous one
520
                                        resourceMapPreviousDate = sysDate;//reset resourceMapPreviousDate to the bigger one
521
                                    } else {
522
                                        int size = resourceMapIds.size();//
523
                                        resourceMapIds.add(size -1, identifier.getValue());//keep the previous one at the end of the list.
524
                                    }
525
                                } else {
526
                                    resourceMapIds.add(identifier.getValue());
527
                                    resourceMapPreviousDate = sysDate;//init resourcemapPreviousDate
528
                                }
529 8291 tao
                            //}
530 7838 tao
                        } else if (correctTimeRange) {
531 8291 tao
                            /*if(sysmeta.getArchived() || sysmeta.getObsoletedBy() != null) {
532 7876 tao
                                //for the archived ids
533
                                if(!otherDeletedIds.isEmpty()) {
534
                                    if(sysDate.getTime() > otherDeletedPreviousDate.getTime()) {
535
                                        otherDeletedIds.add(identifier.getValue());
536
                                        otherDeletedPreviousDate = sysDate;//reset otherDeletedPreviousDate to the bigger one
537
                                    } else {
538
                                        int size = otherDeletedIds.size();
539
                                        otherDeletedIds.add(size-1, identifier.getValue());
540
                                    }
541 7740 tao
                                } else {
542 7876 tao
                                    otherDeletedIds.add(identifier.getValue());
543
                                    otherDeletedPreviousDate = sysDate;//init otherDeletedPreviousDate
544 7740 tao
                                }
545 8291 tao
                            } else {*/
546
                                //for all ids
547 7876 tao
                                if(!otherIds.isEmpty()) {
548
                                    if(sysDate.getTime() > otherPreviousDate.getTime()) {
549
                                        otherIds.add(identifier.getValue());
550
                                        otherPreviousDate = sysDate;//reset otherPreviousDate to the bigger one
551
                                    } else {
552
                                        int size = otherIds.size();
553
                                        otherIds.add(size-1, identifier.getValue());
554
                                    }
555
                                } else {
556
                                    otherIds.add(identifier.getValue());
557
                                    otherPreviousDate = sysDate;//init otherPreviousDate
558
                                }
559 8291 tao
                            //}
560 7607 tao
                        }
561 7806 tao
562 7607 tao
                    }
563
                }
564
            }
565 7806 tao
        }
566 7606 tao
        return ids;
567
    }
568
569
    /*
570 7740 tao
     * If the specified ObjectFormatIdentifier is a resrouce map namespace.
571
     */
572 7786 tao
    public static boolean isResourceMap(ObjectFormatIdentifier formatId) {
573 7740 tao
        boolean isResourceMap = false;
574
        if(formatId != null && resourceMapNamespaces != null) {
575
            for(String namespace : resourceMapNamespaces) {
576
                if(namespace != null && formatId.getValue() != null && !formatId.getValue().trim().equals("") && formatId.getValue().equals(namespace)) {
577
                    isResourceMap = true;
578
                    break;
579
                }
580
            }
581
        }
582
        return isResourceMap;
583
    }
584
585 7793 tao
586 7606 tao
587
    /*
588
     * Generate index for the id.
589
     */
590 8293 tao
    private void generateIndex(String id)  {
591 8343 tao
        //if id is null and sysmeta will be null. If sysmeta is null, it will be caught in solrIndex.update
592
        SystemMetadata sysmeta = getSystemMetadata(id);
593
        Identifier pid = new Identifier();
594
        pid.setValue(id);
595
        solrIndex.update(pid, sysmeta);
596
597 7606 tao
    }
598 7691 tao
599
    /*
600 7876 tao
     * Remove the solr index for the list of ids
601
     */
602 8291 tao
    /*private void removeIndex(List<String> ids) {
603 7876 tao
        if(ids!= null) {
604
            for(String id :ids) {
605 8035 tao
                try {
606
                    removeIndex(id);
607
                } catch (Exception e) {
608
                    IndexEvent event = new IndexEvent();
609
                    Identifier pid = new Identifier();
610
                    pid.setValue(id);
611
                    event.setIdentifier(pid);
612
                    event.setDate(Calendar.getInstance().getTime());
613
                    event.setAction(Event.DELETE);
614
                    String error = "IndexGenerator.index - Metacat Index couldn't remove the index for the id - "+id+" because "+e.getMessage();
615
                    event.setDescription(error);
616
                    try {
617
                        EventlogFactory.createIndexEventLog().write(event);
618
                    } catch (Exception ee) {
619
                        log.error("SolrIndex.insertToIndex - IndexEventLog can't log the index deleting event :"+ee.getMessage());
620
                    }
621
                    log.error(error);
622
                }
623
624 7876 tao
            }
625
        }
626 8291 tao
    }*/
627 7876 tao
628
    /*
629 7857 tao
     * Remove the index for the id
630
     */
631 8291 tao
    /*private void removeIndex(String id) throws ServiceFailure, XPathExpressionException, NotImplemented, NotFound, UnsupportedType, IOException, SolrServerException, SAXException, ParserConfigurationException, OREParserException  {
632 7857 tao
        if(id != null) {
633 8287 tao
            //solrIndex.remove(id);
634 7857 tao
        }
635 8291 tao
    }*/
636 7857 tao
637
    /*
638 7691 tao
     * Initialize the system metadata map
639
     */
640
    private void initSystemMetadataMap() throws FileNotFoundException, ServiceFailure{
641
        int times = 0;
642
        if(systemMetadataMap == null) {
643 7774 tao
            systemMetadataMap = DistributedMapsFactory.getSystemMetadataMap();
644 7691 tao
        }
645
    }
646
647
    /*
648
     * We should call this method after calling initSystemMetadataMap since this method doesn't have the mechanism to wait the readiness of the hazelcast service
649
     */
650
    private void initObjectPathMap() throws FileNotFoundException, ServiceFailure {
651
        if(objectPathMap == null) {
652
            objectPathMap = DistributedMapsFactory.getObjectPathMap();
653
        }
654
    }
655 8293 tao
656
657
658
    /*
659
     * Initialize the index queue
660
     */
661
    private void initIndexQueue() throws FileNotFoundException, ServiceFailure {
662
        if(indexQueue == null) {
663
            indexQueue = DistributedMapsFactory.getIndexQueue();
664
        }
665
    }
666 7691 tao
    /**
667
     * Get an InputStream as the data object for the specific pid.
668
     * @param pid
669
     * @return
670
     * @throws FileNotFoundException
671
     */
672
    private InputStream getDataObject(String pid) throws FileNotFoundException {
673
        Identifier identifier = new Identifier();
674
        identifier.setValue(pid);
675
        String objectPath = objectPathMap.get(identifier);
676
        InputStream data = null;
677
        data = new FileInputStream(objectPath);
678
        return data;
679
680
    }
681
682
    /**
683
     * Get the SystemMetadata for the specified id from the distributed Map.
684
     * The null maybe is returned if there is no system metadata found.
685
     * @param id  the specified id.
686
     * @return the SystemMetadata associated with the id.
687
     */
688
    private SystemMetadata getSystemMetadata(String id) {
689
        SystemMetadata metadata = null;
690
        if(systemMetadataMap != null && id != null) {
691
            Identifier identifier = new Identifier();
692
            identifier.setValue(id);
693
            metadata = systemMetadataMap.get(identifier);
694
        }
695
        return metadata;
696
    }
697
698
    /**
699
     * Get the obsoletes chain of the specified id. The returned list doesn't include
700
     * the specified id itself. The newer version has the lower index number in the list.
701
     * Empty list will be returned if there is no document to be obsoleted by this id.
702
     * @param id
703
     * @return
704
     */
705
    private List<String> getObsoletes(String id) {
706
        List<String> obsoletes = new ArrayList<String>();
707
        while (id != null) {
708
            SystemMetadata metadata = getSystemMetadata(id);
709
            id = null;//set it to be null in order to stop the while loop if the id can't be assinged to a new value in the following code.
710
            if(metadata != null) {
711
                Identifier identifier = metadata.getObsoletes();
712
                if(identifier != null && identifier.getValue() != null && !identifier.getValue().trim().equals("")) {
713
                    obsoletes.add(identifier.getValue());
714
                    id = identifier.getValue();
715
                }
716
            }
717
        }
718
        return obsoletes;
719
    }
720 7788 tao
721
    /**
722
     * Overwrite and do nothing
723
     */
724
    public boolean cancel() {
725
        return true;
726
    }
727 7691 tao
728 7606 tao
}