Project

General

Profile

1
/**
2
 *  Copyright: 2013 Regents of the University of California and the
3
 *             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
import java.io.FileInputStream;
22
import java.io.FileNotFoundException;
23
import java.io.IOException;
24
import java.io.InputStream;
25
import java.util.ArrayList;
26
import java.util.Collections;
27
import java.util.Date;
28
import java.util.List;
29
import java.util.TimerTask;
30

    
31
import javax.xml.parsers.ParserConfigurationException;
32
import javax.xml.xpath.XPathExpressionException;
33

    
34
import org.apache.commons.logging.Log;
35
import org.apache.commons.logging.LogFactory;
36
import org.apache.solr.client.solrj.SolrServerException;
37
import org.dataone.configuration.Settings;
38
import org.dataone.service.exceptions.InvalidRequest;
39
import org.dataone.service.exceptions.InvalidToken;
40
import org.dataone.service.exceptions.NotAuthorized;
41
import org.dataone.service.exceptions.NotFound;
42
import org.dataone.service.exceptions.NotImplemented;
43
import org.dataone.service.exceptions.ServiceFailure;
44
import org.dataone.service.exceptions.UnsupportedType;
45
import org.dataone.service.types.v1.Identifier;
46
import org.dataone.service.types.v1.ObjectFormatIdentifier;
47
import org.dataone.service.types.v2.SystemMetadata;
48
import org.dspace.foresite.OREParserException;
49
import org.xml.sax.SAXException;
50

    
51
import com.hazelcast.core.IMap;
52
import com.hazelcast.core.ISet;
53

    
54
import edu.ucsb.nceas.metacat.common.index.IndexTask;
55
import edu.ucsb.nceas.metacat.common.index.event.IndexEvent;
56
import edu.ucsb.nceas.metacat.index.event.EventlogFactory;
57
import edu.ucsb.nceas.metacat.index.event.IndexEventLogException;
58

    
59

    
60
/**
61
 * A class represents the object to generate massive solr indexes.
62
 * This can happen during an update of Metacat (generating index for all existing documents)
63
 * or regenerate index for those documents
64
 * failing to build index during the insert or update.
65
 * 
66
 * @author tao
67
 *
68
 */
69
public class IndexGeneratorTimerTask extends TimerTask {
70
    
71
    private static final int FIRST =0;
72
    private static final int SECOND =1;
73
    private static final int THIRD = 2;
74
    private static final int FOURTH = 3;
75
    public static final int WAITTIME = 10000;
76
    public static final int MAXWAITNUMBER = 180;
77
    private static final String HTTP = "http://";
78
    private static final String MNAPPENDIX = "/d1/mn";
79
    private static final String RESOURCEMAPPROPERYNAME = "index.resourcemap.namespace";
80
    public static final String WAITIMEPOPERTYNAME = "index.regenerate.start.waitingtime";
81
    public static final String MAXATTEMPTSPROPERTYNAME = "index.regenerate.start.maxattempts";
82
    
83
    
84
    private SolrIndex solrIndex = null;
85
    //private SystemMetadataEventListener systemMetadataListener = null;
86
    private IMap<Identifier, SystemMetadata> systemMetadataMap;
87
    private IMap<Identifier, String> objectPathMap;
88
    private IMap<Identifier, IndexTask> indexQueue;
89
    private Log log = LogFactory.getLog(IndexGeneratorTimerTask.class);
90
    //private MNode mNode = null;
91
    private static List<String> resourceMapNamespaces = null;
92
    
93
    /**
94
     * Constructor
95
     * @param solrIndex
96
     * @param systemMetadataListener
97
     */
98
    public IndexGeneratorTimerTask(SolrIndex solrIndex) {
99
        this.solrIndex = solrIndex;
100
        resourceMapNamespaces = Settings.getConfiguration().getList(RESOURCEMAPPROPERYNAME);
101
        //this.systemMetadataListener = systemMetadataListener;
102
        //this.mNode = new MNode(buildMNBaseURL());
103
      
104
    }
105
    
106
   
107
    
108
    /**
109
     * Build the index for all documents.
110
     * @throws SolrServerException 
111
     * @throws ServiceFailure 
112
     * @throws NotImplemented 
113
     * @throws NotAuthorized 
114
     * @throws InvalidToken 
115
     * @throws InvalidRequest 
116
     * @throws IndexEventLogException 
117
     * @throws IllegalAccessException 
118
     * @throws InstantiationException 
119
     * @throws ClassNotFoundException 
120
     * @throws ParserConfigurationException 
121
     * @throws SAXException 
122
     * @throws IOException 
123
     * @throws UnsupportedType 
124
     * @throws NotFound 
125
     * @throws XPathExpressionException 
126
     * @throws OREParserException 
127
     */
128
    public void indexAll() throws InvalidRequest, InvalidToken,
129
                NotAuthorized, NotImplemented, ServiceFailure, SolrServerException, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException, XPathExpressionException, NotFound, UnsupportedType, IOException, SAXException, ParserConfigurationException, OREParserException {
130
        Date since = null;
131
        Date until = null;
132
        index(since, until);
133
    }
134
    
135
    /**
136
     * Build the index for the docs which have been modified since the specified date.
137
     * @param since
138
     * @throws SolrServerException 
139
     * @throws ServiceFailure 
140
     * @throws NotImplemented 
141
     * @throws NotAuthorized 
142
     * @throws InvalidToken 
143
     * @throws InvalidRequest 
144
     * @throws IndexEventLogException 
145
     * @throws IllegalAccessException 
146
     * @throws InstantiationException 
147
     * @throws ClassNotFoundException 
148
     * @throws ParserConfigurationException 
149
     * @throws SAXException 
150
     * @throws IOException 
151
     * @throws UnsupportedType 
152
     * @throws NotFound 
153
     * @throws XPathExpressionException 
154
     * @throws OREParserException 
155
     */
156
    public void index(Date since) throws InvalidRequest, InvalidToken, 
157
                    NotAuthorized, NotImplemented, ServiceFailure, SolrServerException, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException, XPathExpressionException, NotFound, UnsupportedType, IOException, SAXException, ParserConfigurationException, OREParserException {
158
        Date until = null;
159
        index(since, until);
160
    }
161
    
162
    /**
163
     *  Build the index for the docs which have been modified between the specified date.s
164
     * @param since
165
     * @param until
166
     * @throws SolrServerException 
167
     * @throws ServiceFailure 
168
     * @throws NotImplemented 
169
     * @throws NotAuthorized 
170
     * @throws InvalidToken 
171
     * @throws InvalidRequest 
172
     * @throws IndexEventLogException 
173
     * @throws IllegalAccessException 
174
     * @throws InstantiationException 
175
     * @throws ClassNotFoundException 
176
     * @throws ParserConfigurationException 
177
     * @throws SAXException 
178
     * @throws IOException 
179
     * @throws UnsupportedType 
180
     * @throws NotFound 
181
     * @throws XPathExpressionException 
182
     * @throws OREParserException 
183
     */
184
    public void index(Date since, Date until) throws SolrServerException, InvalidRequest, 
185
                                                InvalidToken, NotAuthorized, NotImplemented, ServiceFailure, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException, XPathExpressionException, NotFound, UnsupportedType, IOException, SAXException, ParserConfigurationException, OREParserException {
186
        Date processedDate = null;
187
        List<String> solrIds = null;
188
        initSystemMetadataMap();
189
        initObjectPathMap();
190
        initIndexQueue();
191
        List[] metacatIds = getMetacatIds(since, until);
192
        List<String> otherMetacatIds = metacatIds[FIRST];
193
        List<String> resourceMapIds =  metacatIds[SECOND];
194
        //List<String> otherDeletedMetacatIds = metacatIds[THIRD];
195
        //List<String> resourceMapDeletedIds = metacatIds[FOURTH];
196
        
197
        //figure out the procesedDate by comparing the last element of otherMetacatIds and resourceMapIds.
198
        List<Long> maxCollection = new ArrayList<Long>();
199
        Date latestOtherId = null;
200
        if (otherMetacatIds != null && !otherMetacatIds.isEmpty()) {
201
            int size = otherMetacatIds.size();
202
            String id = otherMetacatIds.get(size-1);
203
            SystemMetadata sysmeta = getSystemMetadata(id);
204
            latestOtherId = sysmeta.getDateSysMetadataModified();
205
            maxCollection.add(new Long(latestOtherId.getTime()));
206
        }
207
        
208
        /*Date latestDeletedOtherIds = null;
209
        if (otherDeletedMetacatIds != null && !otherDeletedMetacatIds.isEmpty()) {
210
            int size = otherDeletedMetacatIds.size();
211
            String id = otherDeletedMetacatIds.get(size-1);
212
            SystemMetadata sysmeta = getSystemMetadata(id);
213
            latestDeletedOtherIds = sysmeta.getDateSysMetadataModified();
214
            maxCollection.add(new Long(latestDeletedOtherIds.getTime()));
215
        }*/
216
        
217
        Date latestResourceId = null;
218
        if (resourceMapIds != null && !resourceMapIds.isEmpty()) {
219
            int size = resourceMapIds.size();
220
            String id = resourceMapIds.get(size-1);
221
            SystemMetadata sysmeta = getSystemMetadata(id);
222
            latestResourceId = sysmeta.getDateSysMetadataModified();
223
            maxCollection.add(new Long(latestResourceId.getTime()));
224
        }
225
        
226
        /*Date latestDeletedResourceId = null;
227
        if(resourceMapDeletedIds != null && !resourceMapDeletedIds.isEmpty()) {
228
            int size = resourceMapDeletedIds.size();
229
            String id = resourceMapDeletedIds.get(size-1);
230
            SystemMetadata sysmeta = getSystemMetadata(id);
231
            latestDeletedResourceId = sysmeta.getDateSysMetadataModified();
232
            maxCollection.add(new Long(latestDeletedResourceId.getTime()));
233
        }*/
234
        
235
        if(!maxCollection.isEmpty()) {
236
            Long max = Collections.max(maxCollection);
237
            processedDate = new Date(max.longValue());
238
        }
239
        /*if(latestOtherId != null && latestResourceId != null && latestOtherId.getTime() > latestResourceId.getTime()) {
240
            processedDate = latestOtherId;
241
        } else if (latestOtherId != null && latestResourceId != null && latestOtherId.getTime()  <= latestResourceId.getTime()) {
242
            processedDate = latestResourceId;
243
        } else if (latestOtherId == null && latestResourceId != null) {
244
            processedDate = latestResourceId;
245
        } else if (latestOtherId != null && latestResourceId == null) {
246
            processedDate = latestOtherId;
247
        }*/
248
        
249
        
250
        //add the failedPids 
251
        List<IndexEvent> failedEvents = EventlogFactory.createIndexEventLog().getEvents(null, null, null, null);
252
        List<String> failedOtherIds = new ArrayList<String>();
253
        List<String> failedResourceMapIds = new ArrayList<String>();
254
        if(failedEvents != null) {
255
            for(IndexEvent event : failedEvents) {
256
            	String id = event.getIdentifier().getValue();
257
                SystemMetadata sysmeta = getSystemMetadata(id);
258
                if(sysmeta != null) {
259
                    ObjectFormatIdentifier formatId =sysmeta.getFormatId();
260
                    if(formatId != null && formatId.getValue() != null && resourceMapNamespaces != null && isResourceMap(formatId)) {
261
                        failedResourceMapIds.add(id);
262
                    } else {
263
                        failedOtherIds.add(id);
264
                    }
265
                }
266
            }
267
        }
268
        //indexFailedIds(failedOtherIds);
269
        //indexFailedIds(failedResourceMapIds);
270
        
271
        index(failedOtherIds);
272
        index(failedResourceMapIds);
273
        
274
        /*if(!failedOtherIds.isEmpty()) {
275
            failedOtherIds.addAll(otherMetacatIds);
276
        } else {
277
            failedOtherIds = otherMetacatIds;
278
        }
279
        
280
        if(!failedResourceMapIds.isEmpty()) {
281
            failedResourceMapIds.addAll(resourceMapIds);
282
        } else {
283
            failedResourceMapIds = resourceMapIds;
284
        }*/
285
        //log.info("the ids in index_event for reindex ( except the resourcemap)=====================================\n "+failedOtherIds);
286
        //log.info("the resourcemap ids in index_event for reindex =====================================\n "+failedResourceMapIds);
287
        log.info("the metacat ids (except the resource map ids)-----------------------------"+otherMetacatIds);
288
        //logFile(otherMetacatIds, "ids-for-timed-indexing-log");
289
        //log.info("the deleted metacat ids (except the resource map ids)-----------------------------"+otherDeletedMetacatIds);
290
        log.info("the metacat resroucemap ids -----------------------------"+resourceMapIds);
291
        //logFile(resourceMapIds, "ids-for-timed-indexing-log");
292
        //log.info("the deleted metacat resroucemap ids -----------------------------"+resourceMapDeletedIds);
293
        index(otherMetacatIds);
294
        //removeIndex(otherDeletedMetacatIds);
295
        index(resourceMapIds);
296
        //removeIndex(resourceMapDeletedIds);
297
       
298
        //record the timed index.
299
        if(processedDate != null) {
300
            EventlogFactory.createIndexEventLog().setLastProcessDate(processedDate);
301
        }
302
        
303
    }
304
    
305
    /*
306
     * Write the docids which will be indexed into a file. 
307
     */
308
    /*private void logFile(List<String> ids, String fileName)  {
309
        if(ids != null) {
310
            try {
311
                String tempDir = System.getProperty("java.io.tmpdir");
312
                log.info("the temp dir is ===================== "+tempDir);
313
                File idsForIndex = new File(tempDir, fileName);
314
                if(!idsForIndex.exists()) {
315
                    idsForIndex.createNewFile();
316
                } 
317
                
318
                Date date = Calendar.getInstance().getTime();
319
                SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
320
                String dateStr = format.format(date);
321
                List<String> dateList = new ArrayList<String>();
322
                dateList.add(dateStr);
323
                Boolean append = true;
324
                FileUtils.writeLines(idsForIndex, dateList, append);//write time string
325
                FileUtils.writeLines(idsForIndex, ids, append);
326
            } catch (Exception e) {
327
                log.warn("IndexGenerator.logFile - Couldn't log the ids which will be indexed since - "+e.getMessage());
328
            }
329
           
330
        }
331
    }*/
332
    /*
333
     * Doing index
334
     */
335
    private void index(List<String> metacatIds) {
336
        if(metacatIds != null) {
337
            for(String metacatId : metacatIds) {
338
                if(metacatId != null) {
339
                     generateIndex(metacatId);
340
                }
341
            }
342
        }
343
    }
344
    
345
    /*
346
     * Index those ids which failed in the process (We got them from the EventLog)
347
     */
348
    /*private void indexFailedIds(List<IndexEvent> events) {
349
        if(events != null) {
350
            for(IndexEvent event : events) {
351
                if(event != null) {
352
                    Identifier identifier = event.getIdentifier();
353
                    if(identifier != null) {
354
                        String id = identifier.getValue();
355
                        if(id != null) {
356
                            Event action = event.getAction();
357
                            //if (action != null && action.equals(Event.CREATE)) {
358
                                try {
359
                                    generateIndex(id);
360
                                    EventlogFactory.createIndexEventLog().remove(identifier);
361
                                } catch (Exception e) {
362
                                    log.error("IndexGenerator.indexFailedIds - Metacat Index couldn't generate the index for the id - "+id+" because "+e.getMessage());
363
                                }
364
                            
365
                        }
366
                    }
367
                }
368
            }
369
        }
370
    }*/
371
    
372
    public void run() {
373
    
374
        try {
375
            Date since = EventlogFactory.createIndexEventLog().getLastProcessDate();
376
            index(since);
377
        } catch (InvalidRequest e) {
378
            // TODO Auto-generated catch block
379
            //e.printStackTrace();
380
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
381
        } catch (InvalidToken e) {
382
            // TODO Auto-generated catch block
383
            //e.printStackTrace();
384
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
385
        } catch (NotAuthorized e) {
386
            // TODO Auto-generated catch block
387
            //e.printStackTrace();
388
        } catch (NotImplemented e) {
389
            // TODO Auto-generated catch block
390
            //e.printStackTrace();
391
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
392
        } catch (ServiceFailure e) {
393
            // TODO Auto-generated catch block
394
            //e.printStackTrace();
395
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
396
        } catch (SolrServerException e) {
397
            // TODO Auto-generated catch block
398
            //e.printStackTrace();
399
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
400
        } catch (FileNotFoundException e) {
401
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
402
        } catch (ClassNotFoundException e) {
403
            // TODO Auto-generated catch block
404
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
405
        } catch (InstantiationException e) {
406
            // TODO Auto-generated catch block
407
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
408
        } catch (IllegalAccessException e) {
409
            // 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 (IndexEventLogException 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 (XPathExpressionException 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 (NotFound e) {
418
            // TODO Auto-generated catch block
419
            e.printStackTrace();
420
        } catch (UnsupportedType 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 (IOException e) {
424
            // TODO Auto-generated catch block
425
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
426
        } catch (SAXException 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 (ParserConfigurationException 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 (OREParserException 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
        }
436
    }
437
    
438
   
439
    
440
    /*
441
     * Get an array of the list of ids of the metacat which has the systemmetadata modification in the range.
442
     * 
443
     * If since and util are null, it will return all of them.
444
     * 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.
445
     * The reason to split them is when we index the resource map, we need the index of the documents in the resource map ready.
446
     * The last element in the each list has the latest SystemMetadata modification date. But they are not sorted.
447
     */
448
    private List[] getMetacatIds(Date since, Date until) throws InvalidRequest, 
449
                        InvalidToken, NotAuthorized, NotImplemented, ServiceFailure, FileNotFoundException {
450
        String fileName = "ids-from-hazelcast";
451
        List<String> resourceMapIds = new ArrayList();
452
        //List<String> resourceMapDeletedIds = new ArrayList();
453
        List<String> otherIds = new ArrayList();
454
        //List<String> otherDeletedIds = new ArrayList();
455
        List[] ids = new List[2];
456
        ids[FIRST]= otherIds;
457
        ids[SECOND] = resourceMapIds;
458
        //ids[THIRD]  = otherDeletedIds;
459
        //ids[FOURTH] = resourceMapDeletedIds;
460
        ISet<Identifier> metacatIds = DistributedMapsFactory.getIdentifiersSet();
461
        Date otherPreviousDate = null;
462
        Date otherDeletedPreviousDate = null;
463
        Date resourceMapPreviousDate = null;
464
        Date resourceMapDeletedPreviousDate = null;
465
        if(metacatIds != null) {
466
            for(Identifier identifier : metacatIds) {
467
                if(identifier != null && identifier.getValue() != null && !identifier.getValue().equals("")) {
468
                    List<String> idLog = new ArrayList<String>();
469
                    idLog.add(identifier.getValue());
470
                    //logFile(idLog, fileName);
471
                    SystemMetadata sysmeta = getSystemMetadata(identifier.getValue());
472
                    if(sysmeta != null) {
473
                        ObjectFormatIdentifier formatId =sysmeta.getFormatId();
474
                        //System.out.println("the object format id is "+formatId.getValue());
475
                        //System.out.println("the ============ resourcMapNamespaces"+resourceMapNamespaces);
476
                        boolean correctTimeRange = false;
477
                        Date sysDate = sysmeta.getDateSysMetadataModified();
478
                        if(since == null && until == null) {
479
                            correctTimeRange = true;
480
                        } else if (since != null && until == null) {
481
                            if(sysDate.getTime() > since.getTime()) {
482
                                correctTimeRange = true;
483
                            }
484
                        } else if (since == null && until != null) {
485
                            if(sysDate.getTime() < until.getTime()) {
486
                                correctTimeRange = true;
487
                            }
488
                        } else if (since != null && until != null) {
489
                            if(sysDate.getTime() > since.getTime() && sysDate.getTime() < until.getTime()) {
490
                                correctTimeRange = true;
491
                            }
492
                        }
493
                        if(correctTimeRange && formatId != null && formatId.getValue() != null && resourceMapNamespaces != null && isResourceMap(formatId)) {
494
                            //for the resource map
495
                            /*if(sysmeta.getArchived() || sysmeta.getObsoletedBy() != null) {
496
                                //archived ids
497
                                if(!resourceMapDeletedIds.isEmpty()) {
498
                                    if(sysDate.getTime() > resourceMapDeletedPreviousDate.getTime()) {
499
                                        resourceMapDeletedIds.add(identifier.getValue());//append to the end of the list if current is later than the previous one
500
                                        resourceMapDeletedPreviousDate = sysDate;//reset resourceMapPreviousDate to the bigger one
501
                                    } else {
502
                                        int size = resourceMapDeletedIds.size();//
503
                                        resourceMapDeletedIds.add(size -1, identifier.getValue());//keep the previous one at the end of the list.
504
                                    }
505
                                } else {
506
                                    resourceMapDeletedIds.add(identifier.getValue());
507
                                    resourceMapDeletedPreviousDate = sysDate;//init resourcemapPreviousDate
508
                                }
509
                            } else {*/
510
                                // for all ids
511
                                if(!resourceMapIds.isEmpty()) {
512
                                    if(sysDate.getTime() > resourceMapPreviousDate.getTime()) {
513
                                        resourceMapIds.add(identifier.getValue());//append to the end of the list if current is later than the previous one
514
                                        resourceMapPreviousDate = sysDate;//reset resourceMapPreviousDate to the bigger one
515
                                    } else {
516
                                        int size = resourceMapIds.size();//
517
                                        resourceMapIds.add(size -1, identifier.getValue());//keep the previous one at the end of the list.
518
                                    }
519
                                } else {
520
                                    resourceMapIds.add(identifier.getValue());
521
                                    resourceMapPreviousDate = sysDate;//init resourcemapPreviousDate
522
                                }
523
                            //}
524
                        } else if (correctTimeRange) {
525
                            /*if(sysmeta.getArchived() || sysmeta.getObsoletedBy() != null) {
526
                                //for the archived ids
527
                                if(!otherDeletedIds.isEmpty()) {
528
                                    if(sysDate.getTime() > otherDeletedPreviousDate.getTime()) {
529
                                        otherDeletedIds.add(identifier.getValue());
530
                                        otherDeletedPreviousDate = sysDate;//reset otherDeletedPreviousDate to the bigger one
531
                                    } else {
532
                                        int size = otherDeletedIds.size();
533
                                        otherDeletedIds.add(size-1, identifier.getValue());
534
                                    }
535
                                } else {
536
                                    otherDeletedIds.add(identifier.getValue());
537
                                    otherDeletedPreviousDate = sysDate;//init otherDeletedPreviousDate
538
                                }
539
                            } else {*/
540
                                //for all ids
541
                                if(!otherIds.isEmpty()) {
542
                                    if(sysDate.getTime() > otherPreviousDate.getTime()) {
543
                                        otherIds.add(identifier.getValue());
544
                                        otherPreviousDate = sysDate;//reset otherPreviousDate to the bigger one
545
                                    } else {
546
                                        int size = otherIds.size();
547
                                        otherIds.add(size-1, identifier.getValue());
548
                                    }
549
                                } else {
550
                                    otherIds.add(identifier.getValue());
551
                                    otherPreviousDate = sysDate;//init otherPreviousDate
552
                                }
553
                            //}
554
                        }
555
                        
556
                    }
557
                }
558
            }
559
        }
560
        return ids;
561
    }
562
    
563
    /*
564
     * If the specified ObjectFormatIdentifier is a resrouce map namespace.
565
     */
566
    public static boolean isResourceMap(ObjectFormatIdentifier formatId) {
567
        boolean isResourceMap = false;
568
        if(formatId != null && resourceMapNamespaces != null) {
569
            for(String namespace : resourceMapNamespaces) {
570
                if(namespace != null && formatId.getValue() != null && !formatId.getValue().trim().equals("") && formatId.getValue().equals(namespace)) {
571
                    isResourceMap = true;
572
                    break;
573
                }
574
            }
575
        }
576
        return isResourceMap;
577
    }
578
    
579
   
580
    
581
    /*
582
     * Generate index for the id.
583
     */
584
    private void generateIndex(String id)  {
585
        //if id is null and sysmeta will be null. If sysmeta is null, it will be caught in solrIndex.update
586
        SystemMetadata sysmeta = getSystemMetadata(id);
587
        Identifier pid = new Identifier();
588
        pid.setValue(id);
589
        solrIndex.update(pid, sysmeta);
590
 
591
    }
592
    
593
    /*
594
     * Remove the solr index for the list of ids
595
     */
596
    /*private void removeIndex(List<String> ids) {
597
        if(ids!= null) {
598
            for(String id :ids) {
599
                try {
600
                    removeIndex(id);
601
                } catch (Exception e) {
602
                    IndexEvent event = new IndexEvent();
603
                    Identifier pid = new Identifier();
604
                    pid.setValue(id);
605
                    event.setIdentifier(pid);
606
                    event.setDate(Calendar.getInstance().getTime());
607
                    event.setAction(Event.DELETE);
608
                    String error = "IndexGenerator.index - Metacat Index couldn't remove the index for the id - "+id+" because "+e.getMessage();
609
                    event.setDescription(error);
610
                    try {
611
                        EventlogFactory.createIndexEventLog().write(event);
612
                    } catch (Exception ee) {
613
                        log.error("SolrIndex.insertToIndex - IndexEventLog can't log the index deleting event :"+ee.getMessage());
614
                    }
615
                    log.error(error);
616
                }
617
                
618
            }
619
        }
620
    }*/
621
    
622
    /*
623
     * Remove the index for the id
624
     */
625
    /*private void removeIndex(String id) throws ServiceFailure, XPathExpressionException, NotImplemented, NotFound, UnsupportedType, IOException, SolrServerException, SAXException, ParserConfigurationException, OREParserException  {
626
        if(id != null) {
627
            //solrIndex.remove(id);
628
        }
629
    }*/
630
    
631
    /*
632
     * Initialize the system metadata map
633
     */
634
    private void initSystemMetadataMap() throws FileNotFoundException, ServiceFailure{
635
        int times = 0;
636
        if(systemMetadataMap == null) {
637
            systemMetadataMap = DistributedMapsFactory.getSystemMetadataMap();
638
        }
639
    }
640
    
641
    /*
642
     * We should call this method after calling initSystemMetadataMap since this method doesn't have the mechanism to wait the readiness of the hazelcast service
643
     */
644
    private void initObjectPathMap() throws FileNotFoundException, ServiceFailure {
645
        if(objectPathMap == null) {
646
            objectPathMap = DistributedMapsFactory.getObjectPathMap();
647
        }
648
    }
649
    
650
    
651
    
652
    /*
653
     * Initialize the index queue
654
     */
655
    private void initIndexQueue() throws FileNotFoundException, ServiceFailure {
656
        if(indexQueue == null) {
657
            indexQueue = DistributedMapsFactory.getIndexQueue();
658
        }
659
    }
660
    /**
661
     * Get an InputStream as the data object for the specific pid.
662
     * @param pid
663
     * @return
664
     * @throws FileNotFoundException
665
     */
666
    private InputStream getDataObject(String pid) throws FileNotFoundException {
667
        Identifier identifier = new Identifier();
668
        identifier.setValue(pid);
669
        String objectPath = objectPathMap.get(identifier);
670
        InputStream data = null;
671
        data = new FileInputStream(objectPath);
672
        return data;
673

    
674
    }
675
    
676
    /**
677
     * Get the SystemMetadata for the specified id from the distributed Map.
678
     * The null maybe is returned if there is no system metadata found.
679
     * @param id  the specified id.
680
     * @return the SystemMetadata associated with the id.
681
     */
682
    private SystemMetadata getSystemMetadata(String id) {
683
        SystemMetadata metadata = null;
684
        if(systemMetadataMap != null && id != null) {
685
            Identifier identifier = new Identifier();
686
            identifier.setValue(id);
687
            metadata = systemMetadataMap.get(identifier);
688
        }
689
        return metadata;
690
    }
691
    
692
    /**
693
     * Get the obsoletes chain of the specified id. The returned list doesn't include
694
     * the specified id itself. The newer version has the lower index number in the list.
695
     * Empty list will be returned if there is no document to be obsoleted by this id.
696
     * @param id
697
     * @return
698
     */
699
    private List<String> getObsoletes(String id) {
700
        List<String> obsoletes = new ArrayList<String>();
701
        while (id != null) {
702
            SystemMetadata metadata = getSystemMetadata(id);
703
            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.
704
            if(metadata != null) {
705
                Identifier identifier = metadata.getObsoletes();
706
                if(identifier != null && identifier.getValue() != null && !identifier.getValue().trim().equals("")) {
707
                    obsoletes.add(identifier.getValue());
708
                    id = identifier.getValue();
709
                } 
710
            } 
711
        }
712
        return obsoletes;
713
    }
714
    
715
    /**
716
     * Overwrite and do nothing
717
     */
718
    public boolean cancel() {
719
        return true;
720
    }
721

    
722
}
(3-3/6)