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.File;
22
import java.io.FileInputStream;
23
import java.io.FileNotFoundException;
24
import java.io.IOException;
25
import java.io.InputStream;
26
import java.text.SimpleDateFormat;
27
import java.util.ArrayList;
28
import java.util.Calendar;
29
import java.util.Collections;
30
import java.util.Date;
31
import java.util.List;
32
import java.util.TimerTask;
33

    
34
import javax.xml.parsers.ParserConfigurationException;
35
import javax.xml.xpath.XPathExpressionException;
36

    
37
import org.apache.commons.io.FileUtils;
38
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
import org.dataone.service.exceptions.NotFound;
46
import org.dataone.service.exceptions.NotImplemented;
47
import org.dataone.service.exceptions.ServiceFailure;
48
import org.dataone.service.exceptions.UnsupportedType;
49
import org.dataone.service.types.v1.Event;
50
import org.dataone.service.types.v1.Identifier;
51
import org.dataone.service.types.v1.ObjectFormatIdentifier;
52
import org.dataone.service.types.v1.SystemMetadata;
53
import org.dspace.foresite.OREParserException;
54
import org.xml.sax.SAXException;
55

    
56
import com.hazelcast.core.IMap;
57
import com.hazelcast.core.ISet;
58

    
59
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
60
import edu.ucsb.nceas.metacat.common.index.IndexTask;
61
import edu.ucsb.nceas.metacat.common.index.event.IndexEvent;
62
import edu.ucsb.nceas.metacat.index.event.EventlogFactory;
63
import edu.ucsb.nceas.metacat.index.event.IndexEventLogException;
64

    
65

    
66
/**
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
public class IndexGeneratorTimerTask extends TimerTask {
76
    
77
    private static final int FIRST =0;
78
    private static final int SECOND =1;
79
    private static final int THIRD = 2;
80
    private static final int FOURTH = 3;
81
    public static final int WAITTIME = 10000;
82
    public static final int MAXWAITNUMBER = 180;
83
    private static final String HTTP = "http://";
84
    private static final String MNAPPENDIX = "/d1/mn";
85
    private static final String RESOURCEMAPPROPERYNAME = "index.resourcemap.namespace";
86
    public static final String WAITIMEPOPERTYNAME = "index.regenerate.start.waitingtime";
87
    public static final String MAXATTEMPTSPROPERTYNAME = "index.regenerate.start.maxattempts";
88
    
89
    
90
    private SolrIndex solrIndex = null;
91
    //private SystemMetadataEventListener systemMetadataListener = null;
92
    private IMap<Identifier, SystemMetadata> systemMetadataMap;
93
    private IMap<Identifier, String> objectPathMap;
94
    private IMap<Identifier, IndexTask> indexQueue;
95
    private Log log = LogFactory.getLog(IndexGeneratorTimerTask.class);
96
    //private MNode mNode = null;
97
    private static List<String> resourceMapNamespaces = null;
98
    
99
    /**
100
     * Constructor
101
     * @param solrIndex
102
     * @param systemMetadataListener
103
     */
104
    public IndexGeneratorTimerTask(SolrIndex solrIndex) {
105
        this.solrIndex = solrIndex;
106
        resourceMapNamespaces = Settings.getConfiguration().getList(RESOURCEMAPPROPERYNAME);
107
        //this.systemMetadataListener = systemMetadataListener;
108
        //this.mNode = new MNode(buildMNBaseURL());
109
      
110
    }
111
    
112
   
113
    
114
    /**
115
     * Build the index for all documents.
116
     * @throws SolrServerException 
117
     * @throws ServiceFailure 
118
     * @throws NotImplemented 
119
     * @throws NotAuthorized 
120
     * @throws InvalidToken 
121
     * @throws InvalidRequest 
122
     * @throws IndexEventLogException 
123
     * @throws IllegalAccessException 
124
     * @throws InstantiationException 
125
     * @throws ClassNotFoundException 
126
     * @throws ParserConfigurationException 
127
     * @throws SAXException 
128
     * @throws IOException 
129
     * @throws UnsupportedType 
130
     * @throws NotFound 
131
     * @throws XPathExpressionException 
132
     * @throws OREParserException 
133
     */
134
    public void indexAll() throws InvalidRequest, InvalidToken,
135
                NotAuthorized, NotImplemented, ServiceFailure, SolrServerException, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException, XPathExpressionException, NotFound, UnsupportedType, IOException, SAXException, ParserConfigurationException, OREParserException {
136
        Date since = null;
137
        Date until = null;
138
        index(since, until);
139
    }
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
     * @throws IndexEventLogException 
151
     * @throws IllegalAccessException 
152
     * @throws InstantiationException 
153
     * @throws ClassNotFoundException 
154
     * @throws ParserConfigurationException 
155
     * @throws SAXException 
156
     * @throws IOException 
157
     * @throws UnsupportedType 
158
     * @throws NotFound 
159
     * @throws XPathExpressionException 
160
     * @throws OREParserException 
161
     */
162
    public void index(Date since) throws InvalidRequest, InvalidToken, 
163
                    NotAuthorized, NotImplemented, ServiceFailure, SolrServerException, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException, XPathExpressionException, NotFound, UnsupportedType, IOException, SAXException, ParserConfigurationException, OREParserException {
164
        Date until = null;
165
        index(since, until);
166
    }
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
     * @throws IndexEventLogException 
179
     * @throws IllegalAccessException 
180
     * @throws InstantiationException 
181
     * @throws ClassNotFoundException 
182
     * @throws ParserConfigurationException 
183
     * @throws SAXException 
184
     * @throws IOException 
185
     * @throws UnsupportedType 
186
     * @throws NotFound 
187
     * @throws XPathExpressionException 
188
     * @throws OREParserException 
189
     */
190
    public void index(Date since, Date until) throws SolrServerException, InvalidRequest, 
191
                                                InvalidToken, NotAuthorized, NotImplemented, ServiceFailure, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException, XPathExpressionException, NotFound, UnsupportedType, IOException, SAXException, ParserConfigurationException, OREParserException {
192
        Date processedDate = null;
193
        List<String> solrIds = null;
194
        initSystemMetadataMap();
195
        initObjectPathMap();
196
        initIndexQueue();
197
        List[] metacatIds = getMetacatIds(since, until);
198
        List<String> otherMetacatIds = metacatIds[FIRST];
199
        List<String> resourceMapIds =  metacatIds[SECOND];
200
        //List<String> otherDeletedMetacatIds = metacatIds[THIRD];
201
        //List<String> resourceMapDeletedIds = metacatIds[FOURTH];
202
        
203
        //figure out the procesedDate by comparing the last element of otherMetacatIds and resourceMapIds.
204
        List<Long> maxCollection = new ArrayList<Long>();
205
        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
            maxCollection.add(new Long(latestOtherId.getTime()));
212
        }
213
        
214
        /*Date latestDeletedOtherIds = null;
215
        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
        }*/
222
        
223
        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
            maxCollection.add(new Long(latestResourceId.getTime()));
230
        }
231
        
232
        /*Date latestDeletedResourceId = null;
233
        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
        }*/
240
        
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
            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
        }*/
254
        
255
        
256
        //add the failedPids 
257
        List<IndexEvent> failedEvents = EventlogFactory.createIndexEventLog().getEvents(null, null, null, null);
258
        List<String> failedOtherIds = new ArrayList<String>();
259
        List<String> failedResourceMapIds = new ArrayList<String>();
260
        if(failedEvents != null) {
261
            for(IndexEvent event : failedEvents) {
262
            	String id = event.getIdentifier().getValue();
263
                SystemMetadata sysmeta = getSystemMetadata(id);
264
                if(sysmeta != null) {
265
                    ObjectFormatIdentifier formatId =sysmeta.getFormatId();
266
                    if(formatId != null && formatId.getValue() != null && resourceMapNamespaces != null && isResourceMap(formatId)) {
267
                        failedResourceMapIds.add(id);
268
                    } else {
269
                        failedOtherIds.add(id);
270
                    }
271
                }
272
            }
273
        }
274
        //indexFailedIds(failedOtherIds);
275
        //indexFailedIds(failedResourceMapIds);
276
        
277
        index(failedOtherIds);
278
        index(failedResourceMapIds);
279
        
280
        /*if(!failedOtherIds.isEmpty()) {
281
            failedOtherIds.addAll(otherMetacatIds);
282
        } else {
283
            failedOtherIds = otherMetacatIds;
284
        }
285
        
286
        if(!failedResourceMapIds.isEmpty()) {
287
            failedResourceMapIds.addAll(resourceMapIds);
288
        } else {
289
            failedResourceMapIds = resourceMapIds;
290
        }*/
291
        //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
        log.info("the metacat ids (except the resource map ids)-----------------------------"+otherMetacatIds);
294
        //logFile(otherMetacatIds, "ids-for-timed-indexing-log");
295
        //log.info("the deleted metacat ids (except the resource map ids)-----------------------------"+otherDeletedMetacatIds);
296
        log.info("the metacat resroucemap ids -----------------------------"+resourceMapIds);
297
        //logFile(resourceMapIds, "ids-for-timed-indexing-log");
298
        //log.info("the deleted metacat resroucemap ids -----------------------------"+resourceMapDeletedIds);
299
        index(otherMetacatIds);
300
        //removeIndex(otherDeletedMetacatIds);
301
        index(resourceMapIds);
302
        //removeIndex(resourceMapDeletedIds);
303
       
304
        //record the timed index.
305
        if(processedDate != null) {
306
            EventlogFactory.createIndexEventLog().setLastProcessDate(processedDate);
307
        }
308
        
309
    }
310
    
311
    /*
312
     * Write the docids which will be indexed into a file. 
313
     */
314
    /*private void logFile(List<String> ids, String fileName)  {
315
        if(ids != null) {
316
            try {
317
                String tempDir = System.getProperty("java.io.tmpdir");
318
                log.info("the temp dir is ===================== "+tempDir);
319
                File idsForIndex = new File(tempDir, fileName);
320
                if(!idsForIndex.exists()) {
321
                    idsForIndex.createNewFile();
322
                } 
323
                
324
                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
            }
335
           
336
        }
337
    }*/
338
    /*
339
     * Doing index
340
     */
341
    private void index(List<String> metacatIds) {
342
        if(metacatIds != null) {
343
            for(String metacatId : metacatIds) {
344
                if(metacatId != null) {
345
                     generateIndex(metacatId);
346
                }
347
            }
348
        }
349
    }
350
    
351
    /*
352
     * Index those ids which failed in the process (We got them from the EventLog)
353
     */
354
    /*private void indexFailedIds(List<IndexEvent> events) {
355
        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
                            //if (action != null && action.equals(Event.CREATE)) {
364
                                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
                            
371
                        }
372
                    }
373
                }
374
            }
375
        }
376
    }*/
377
    
378
    public void run() {
379
    
380
        try {
381
            Date since = EventlogFactory.createIndexEventLog().getLastProcessDate();
382
            index(since);
383
        } 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
        } 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
        } catch (ClassNotFoundException 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 (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
        } 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
        } 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
        }
442
    }
443
    
444
   
445
    
446
    /*
447
     * 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
     * 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
     * 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
     */
454
    private List[] getMetacatIds(Date since, Date until) throws InvalidRequest, 
455
                        InvalidToken, NotAuthorized, NotImplemented, ServiceFailure, FileNotFoundException {
456
        String fileName = "ids-from-hazelcast";
457
        List<String> resourceMapIds = new ArrayList();
458
        //List<String> resourceMapDeletedIds = new ArrayList();
459
        List<String> otherIds = new ArrayList();
460
        //List<String> otherDeletedIds = new ArrayList();
461
        List[] ids = new List[2];
462
        ids[FIRST]= otherIds;
463
        ids[SECOND] = resourceMapIds;
464
        //ids[THIRD]  = otherDeletedIds;
465
        //ids[FOURTH] = resourceMapDeletedIds;
466
        ISet<Identifier> metacatIds = DistributedMapsFactory.getIdentifiersSet();
467
        Date otherPreviousDate = null;
468
        Date otherDeletedPreviousDate = null;
469
        Date resourceMapPreviousDate = null;
470
        Date resourceMapDeletedPreviousDate = null;
471
        if(metacatIds != null) {
472
            for(Identifier identifier : metacatIds) {
473
                if(identifier != null && identifier.getValue() != null && !identifier.getValue().equals("")) {
474
                    List<String> idLog = new ArrayList<String>();
475
                    idLog.add(identifier.getValue());
476
                    //logFile(idLog, fileName);
477
                    SystemMetadata sysmeta = getSystemMetadata(identifier.getValue());
478
                    if(sysmeta != null) {
479
                        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
                            if(sysDate.getTime() > since.getTime()) {
488
                                correctTimeRange = true;
489
                            }
490
                        } else if (since == null && until != null) {
491
                            if(sysDate.getTime() < until.getTime()) {
492
                                correctTimeRange = true;
493
                            }
494
                        } else if (since != null && until != null) {
495
                            if(sysDate.getTime() > since.getTime() && sysDate.getTime() < until.getTime()) {
496
                                correctTimeRange = true;
497
                            }
498
                        }
499
                        if(correctTimeRange && formatId != null && formatId.getValue() != null && resourceMapNamespaces != null && isResourceMap(formatId)) {
500
                            //for the resource map
501
                            /*if(sysmeta.getArchived() || sysmeta.getObsoletedBy() != null) {
502
                                //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
                                } else {
512
                                    resourceMapDeletedIds.add(identifier.getValue());
513
                                    resourceMapDeletedPreviousDate = sysDate;//init resourcemapPreviousDate
514
                                }
515
                            } else {*/
516
                                // for all ids
517
                                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
                            //}
530
                        } else if (correctTimeRange) {
531
                            /*if(sysmeta.getArchived() || sysmeta.getObsoletedBy() != null) {
532
                                //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
                                } else {
542
                                    otherDeletedIds.add(identifier.getValue());
543
                                    otherDeletedPreviousDate = sysDate;//init otherDeletedPreviousDate
544
                                }
545
                            } else {*/
546
                                //for all ids
547
                                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
                            //}
560
                        }
561
                        
562
                    }
563
                }
564
            }
565
        }
566
        return ids;
567
    }
568
    
569
    /*
570
     * If the specified ObjectFormatIdentifier is a resrouce map namespace.
571
     */
572
    public static boolean isResourceMap(ObjectFormatIdentifier formatId) {
573
        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
   
586
    
587
    /*
588
     * Generate index for the id.
589
     */
590
    private void generateIndex(String id)  {
591
        //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
    }
598
    
599
    /*
600
     * Remove the solr index for the list of ids
601
     */
602
    /*private void removeIndex(List<String> ids) {
603
        if(ids!= null) {
604
            for(String id :ids) {
605
                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
            }
625
        }
626
    }*/
627
    
628
    /*
629
     * Remove the index for the id
630
     */
631
    /*private void removeIndex(String id) throws ServiceFailure, XPathExpressionException, NotImplemented, NotFound, UnsupportedType, IOException, SolrServerException, SAXException, ParserConfigurationException, OREParserException  {
632
        if(id != null) {
633
            //solrIndex.remove(id);
634
        }
635
    }*/
636
    
637
    /*
638
     * Initialize the system metadata map
639
     */
640
    private void initSystemMetadataMap() throws FileNotFoundException, ServiceFailure{
641
        int times = 0;
642
        if(systemMetadataMap == null) {
643
            systemMetadataMap = DistributedMapsFactory.getSystemMetadataMap();
644
        }
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
    
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
    /**
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
    
721
    /**
722
     * Overwrite and do nothing
723
     */
724
    public boolean cancel() {
725
        return true;
726
    }
727

    
728
}
(3-3/6)