Project

General

Profile

1 7606 tao
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A class that gets Accession Number, check for uniqueness
4
 *             and register it into db
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Jivka Bojilova, Matt Jones
8
 *
9
 *   '$Author: leinfelder $'
10
 *     '$Date: 2011-11-02 20:40:12 -0700 (Wed, 02 Nov 2011) $'
11
 * '$Revision: 6595 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27
package edu.ucsb.nceas.metacat.index;
28
29 7691 tao
import java.io.FileInputStream;
30
import java.io.FileNotFoundException;
31 7606 tao
import java.io.InputStream;
32
import java.util.ArrayList;
33 7802 tao
import java.util.Calendar;
34 7606 tao
import java.util.Date;
35
import java.util.List;
36 7788 tao
import java.util.TimerTask;
37 7606 tao
import java.util.Vector;
38
39
import org.apache.commons.logging.Log;
40
import org.apache.commons.logging.LogFactory;
41
import org.apache.solr.client.solrj.SolrServerException;
42
import org.dataone.client.MNode;
43
import org.dataone.configuration.Settings;
44
import org.dataone.service.exceptions.InvalidRequest;
45
import org.dataone.service.exceptions.InvalidToken;
46
import org.dataone.service.exceptions.NotAuthorized;
47
import org.dataone.service.exceptions.NotImplemented;
48
import org.dataone.service.exceptions.ServiceFailure;
49
import org.dataone.service.types.v1.Identifier;
50 7740 tao
import org.dataone.service.types.v1.ObjectFormatIdentifier;
51 7607 tao
import org.dataone.service.types.v1.ObjectInfo;
52 7606 tao
import org.dataone.service.types.v1.ObjectList;
53
import org.dataone.service.types.v1.SystemMetadata;
54
55 7691 tao
import com.hazelcast.core.IMap;
56 7793 tao
import com.hazelcast.core.ISet;
57 7606 tao
58 7802 tao
import edu.ucsb.nceas.metacat.index.event.EventlogFactory;
59
import edu.ucsb.nceas.metacat.index.event.IndexEvent;
60 7806 tao
import edu.ucsb.nceas.metacat.index.event.IndexEventLogException;
61 7691 tao
62 7802 tao
63 7606 tao
/**
64
 * A class represents the object to generate massive solr indexes.
65
 * This can happen during an update of Metacat (generating index for all existing documents)
66
 * or regenerate index for those documents
67
 * failing to build index during the insert or update.
68
 *
69
 * @author tao
70
 *
71
 */
72 7788 tao
public class IndexGenerator extends TimerTask {
73 7606 tao
74 7740 tao
    private static final int FIRST =0;
75
    private static final int SECOND =1;
76 7774 tao
    public static final int WAITTIME = 10000;
77
    public static final int MAXWAITNUMBER = 180;
78 7606 tao
    private static final String HTTP = "http://";
79
    private static final String MNAPPENDIX = "/d1/mn";
80 7740 tao
    private static final String RESOURCEMAPPROPERYNAME = "index.resourcemap.namespace";
81 7774 tao
    public static final String WAITIMEPOPERTYNAME = "index.regenerate.start.waitingtime";
82
    public static final String MAXATTEMPTSPROPERTYNAME = "index.regenerate.start.maxattempts";
83 7748 tao
84
    private static int waitingTime = WAITTIME;
85
    private static int maxAttempts = MAXWAITNUMBER;
86
87 7606 tao
    private SolrIndex solrIndex = null;
88 7691 tao
    //private SystemMetadataEventListener systemMetadataListener = null;
89
    private IMap<Identifier, SystemMetadata> systemMetadataMap;
90
    private IMap<Identifier, String> objectPathMap;
91 7606 tao
    private Log log = LogFactory.getLog(IndexGenerator.class);
92 7793 tao
    //private MNode mNode = null;
93 7786 tao
    private static List<String> resourceMapNamespaces = null;
94 7606 tao
95
    /**
96
     * Constructor
97
     * @param solrIndex
98
     * @param systemMetadataListener
99
     */
100 7691 tao
    public IndexGenerator(SolrIndex solrIndex) {
101 7606 tao
        this.solrIndex = solrIndex;
102 7740 tao
        resourceMapNamespaces = Settings.getConfiguration().getList(RESOURCEMAPPROPERYNAME);
103 7691 tao
        //this.systemMetadataListener = systemMetadataListener;
104 7793 tao
        //this.mNode = new MNode(buildMNBaseURL());
105 7748 tao
        try {
106
            waitingTime = Settings.getConfiguration().getInt(WAITIMEPOPERTYNAME);
107
            maxAttempts = Settings.getConfiguration().getInt(MAXATTEMPTSPROPERTYNAME);
108
        } catch (Exception e) {
109
            log.warn("IndexGenerator.constructor - couldn't read the waiting time or maxattempts from the metacat.properties file since : "+e.getMessage()+". Default values will be used");
110
            waitingTime = WAITTIME;
111
            maxAttempts = MAXWAITNUMBER;
112
        }
113 7606 tao
    }
114
115
    /**
116
     * Build the index for all documents in Metacat without overwriting.
117
     * @throws SolrServerException
118
     * @throws ServiceFailure
119
     * @throws NotImplemented
120
     * @throws NotAuthorized
121
     * @throws InvalidToken
122
     * @throws InvalidRequest
123 7806 tao
     * @throws IndexEventLogException
124
     * @throws IllegalAccessException
125
     * @throws InstantiationException
126
     * @throws ClassNotFoundException
127 7606 tao
     */
128 7806 tao
    /*public void indexAll() throws InvalidRequest, InvalidToken, NotAuthorized,
129
                            NotImplemented, ServiceFailure, SolrServerException, FileNotFoundException, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException {
130 7606 tao
        boolean force = false;
131
        indexAll(force);
132 7806 tao
    }*/
133 7606 tao
134
    /**
135 7806 tao
     * Build the index for all documents.
136 7606 tao
     * @throws SolrServerException
137
     * @throws ServiceFailure
138
     * @throws NotImplemented
139
     * @throws NotAuthorized
140
     * @throws InvalidToken
141
     * @throws InvalidRequest
142 7806 tao
     * @throws IndexEventLogException
143
     * @throws IllegalAccessException
144
     * @throws InstantiationException
145
     * @throws ClassNotFoundException
146 7606 tao
     */
147 7806 tao
    public void indexAll() throws InvalidRequest, InvalidToken,
148
                NotAuthorized, NotImplemented, ServiceFailure, SolrServerException, FileNotFoundException, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException {
149 7606 tao
        Date since = null;
150
        Date until = null;
151 7806 tao
        index(since, until);
152 7606 tao
    }
153
154
    /**
155
     * Build the index for the docs which have been modified since the specified date.
156
     * @param since
157
     * @throws SolrServerException
158
     * @throws ServiceFailure
159
     * @throws NotImplemented
160
     * @throws NotAuthorized
161
     * @throws InvalidToken
162
     * @throws InvalidRequest
163 7806 tao
     * @throws IndexEventLogException
164
     * @throws IllegalAccessException
165
     * @throws InstantiationException
166
     * @throws ClassNotFoundException
167 7606 tao
     */
168 7806 tao
    public void index(Date since) throws InvalidRequest, InvalidToken,
169
                    NotAuthorized, NotImplemented, ServiceFailure, SolrServerException, FileNotFoundException, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException {
170 7606 tao
        Date until = null;
171 7806 tao
        index(since, until);
172 7606 tao
    }
173
174
    /**
175
     *  Build the index for the docs which have been modified between the specified date.s
176
     * @param since
177
     * @param until
178
     * @throws SolrServerException
179
     * @throws ServiceFailure
180
     * @throws NotImplemented
181
     * @throws NotAuthorized
182
     * @throws InvalidToken
183
     * @throws InvalidRequest
184 7691 tao
     * @throws FileNotFoundException
185 7806 tao
     * @throws IndexEventLogException
186
     * @throws IllegalAccessException
187
     * @throws InstantiationException
188
     * @throws ClassNotFoundException
189 7606 tao
     */
190 7806 tao
    public void index(Date since, Date until) throws SolrServerException, InvalidRequest,
191
                                                InvalidToken, NotAuthorized, NotImplemented, ServiceFailure, FileNotFoundException, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException {
192
        Date processedDate = null;
193 7606 tao
        List<String> solrIds = null;
194 7740 tao
        initSystemMetadataMap();
195
        initObjectPathMap();
196 7806 tao
        List[] metacatIds = getMetacatIds(since, until);
197
        List<String> otherMetacatIds = metacatIds[FIRST];
198
        List<String> resourceMapIds =  metacatIds[SECOND];
199
200
        //figure out the procesedDate by comparing the last element of otherMetacatIds and resourceMapIds.
201
        Date latestOtherId = null;
202
        if (otherMetacatIds != null && !otherMetacatIds.isEmpty()) {
203
            int size = otherMetacatIds.size();
204
            String id = otherMetacatIds.get(size-1);
205
            SystemMetadata sysmeta = getSystemMetadata(id);
206
            latestOtherId = sysmeta.getDateSysMetadataModified();
207 7606 tao
        }
208 7806 tao
        Date latestResourceId = null;
209
        if (resourceMapIds != null && !resourceMapIds.isEmpty()) {
210
            int size = resourceMapIds.size();
211
            String id = resourceMapIds.get(size-1);
212
            SystemMetadata sysmeta = getSystemMetadata(id);
213
            latestResourceId = sysmeta.getDateSysMetadataModified();
214
        }
215
        if(latestOtherId != null && latestResourceId != null && latestOtherId.getTime() > latestResourceId.getTime()) {
216
            processedDate = latestOtherId;
217
        } else if (latestOtherId != null && latestResourceId != null && latestOtherId.getTime()  <= latestResourceId.getTime()) {
218
            processedDate = latestResourceId;
219
        } else if (latestOtherId == null && latestResourceId != null) {
220
            processedDate = latestResourceId;
221
        } else if (latestOtherId != null && latestResourceId == null) {
222
            processedDate = latestOtherId;
223
        }
224
225
        //add the failedPids
226
        List<String> failedPids = EventlogFactory.createIndexEventLog().getFailedPids();
227
        List<String> failedOtherIds = new ArrayList<String>();
228
        List<String> failedResourceMapIds = new ArrayList<String>();
229
        if(failedPids != null) {
230
            for(String id : failedPids) {
231
                SystemMetadata sysmeta = getSystemMetadata(id);
232
                if(sysmeta != null && !sysmeta.getArchived()) {
233
                    ObjectFormatIdentifier formatId =sysmeta.getFormatId();
234
                    if(formatId != null && formatId.getValue() != null && resourceMapNamespaces != null && isResourceMap(formatId)) {
235
                        failedResourceMapIds.add(id);
236
                    } else {
237
                        failedOtherIds.add(id);
238
                    }
239
                }
240
            }
241
        }
242
243
        if(!failedOtherIds.isEmpty()) {
244
            failedOtherIds.addAll(otherMetacatIds);
245
        } else {
246
            failedOtherIds = otherMetacatIds;
247
        }
248
249
        if(!failedResourceMapIds.isEmpty()) {
250
            failedResourceMapIds.addAll(resourceMapIds);
251
        } else {
252
            failedResourceMapIds = resourceMapIds;
253
        }
254
255
        log.info("the metacat ids (exception resource map -----------------------------"+failedOtherIds);
256
        log.info("the metacat resroucemap ids -----------------------------"+failedResourceMapIds);
257
        index(failedOtherIds);
258
        index(failedResourceMapIds);
259 7740 tao
260 7806 tao
        //record the timed index.
261
        if(processedDate != null) {
262
            EventlogFactory.createIndexEventLog().setLastProcessDate(processedDate);
263
        }
264 7734 tao
265 7740 tao
    }
266
267
    /*
268 7806 tao
     * Doing index
269 7740 tao
     */
270 7806 tao
    private void index(List<String> metacatIds) {
271 7688 tao
        if(metacatIds != null) {
272 7606 tao
            for(String metacatId : metacatIds) {
273
                if(metacatId != null) {
274
                        try {
275
                            generateIndex(metacatId);
276
                        } catch (Exception e) {
277
                            log.error("IndexGenerator.index - Metacat Index couldn't generate the index for the id - "+metacatId+" because "+e.getMessage());
278
                        }
279
280 7806 tao
281 7606 tao
                }
282
            }
283 7688 tao
        }
284 7606 tao
    }
285
286 7613 tao
    public void run() {
287 7805 tao
        /*IndexEvent event = new IndexEvent();
288 7802 tao
        event.setDate(Calendar.getInstance().getTime());
289
        event.setType(IndexEvent.STARTTIMEDINDEX);
290
        event.setDescription("Start the timed index job");
291 7613 tao
        try {
292 7802 tao
            EventlogFactory.createIndexEventLog().write(event);
293
        } catch (Exception e) {
294
            log.error("IndexGenerator.run - IndexEventLog can't log the timed indexing start event :"+e.getMessage());
295 7805 tao
        }*/
296 7802 tao
        try {
297 7806 tao
            Date since = EventlogFactory.createIndexEventLog().getLastProcessDate();
298
            index(since);
299 7613 tao
        } catch (InvalidRequest e) {
300
            // TODO Auto-generated catch block
301
            //e.printStackTrace();
302
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
303
        } catch (InvalidToken e) {
304
            // TODO Auto-generated catch block
305
            //e.printStackTrace();
306
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
307
        } catch (NotAuthorized e) {
308
            // TODO Auto-generated catch block
309
            //e.printStackTrace();
310
        } catch (NotImplemented e) {
311
            // TODO Auto-generated catch block
312
            //e.printStackTrace();
313
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
314
        } catch (ServiceFailure e) {
315
            // TODO Auto-generated catch block
316
            //e.printStackTrace();
317
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
318
        } catch (SolrServerException e) {
319
            // TODO Auto-generated catch block
320
            //e.printStackTrace();
321
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
322 7691 tao
        } catch (FileNotFoundException e) {
323
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
324 7613 tao
        }
325 7805 tao
        /*event.setDate(Calendar.getInstance().getTime());
326 7802 tao
        event.setType(IndexEvent.FINISHTIMEDINDEX);
327
        event.setDescription("Finish the timed index job");
328
        try {
329
            EventlogFactory.createIndexEventLog().write(event);
330
        } catch (Exception e) {
331
            log.error("IndexGenerator.run - IndexEventLog can't log the timed indexing finish event :"+e.getMessage());
332 7806 tao
        }*/ catch (ClassNotFoundException e) {
333
            // TODO Auto-generated catch block
334
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
335
        } catch (InstantiationException e) {
336
            // TODO Auto-generated catch block
337
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
338
        } catch (IllegalAccessException e) {
339
            // TODO Auto-generated catch block
340
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
341
        } catch (IndexEventLogException e) {
342
            // TODO Auto-generated catch block
343
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
344
        }
345 7613 tao
    }
346
347 7606 tao
    /*
348
     * Get the indexed ids list from the solr server.
349
     * An empty list will be returned if there is no ids.
350
     */
351
    private List<String> getSolrDocIds() throws SolrServerException {
352
        List<String> ids = solrIndex.getSolrIds();
353
        return ids;
354
    }
355
356
    /*
357 7806 tao
     * Get an array of the list of ids of the metacat which has the systemmetadata modification in the range.
358
     *
359
     * If since and util are null, it will return all of them.
360 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.
361 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.
362
     * The last element in the each list has the latest SystemMetadata modification date. But they are not sorted.
363 7606 tao
     */
364 7793 tao
    private List[] getMetacatIds(Date since, Date until) throws InvalidRequest,
365
                        InvalidToken, NotAuthorized, NotImplemented, ServiceFailure, FileNotFoundException {
366 7740 tao
367
        List<String> resourceMapIds = new ArrayList();
368
        List<String> otherIds = new ArrayList();
369
        List[] ids = new List[2];
370
        ids[FIRST]= otherIds;
371
        ids[SECOND] = resourceMapIds;
372 7793 tao
        ISet<Identifier> metacatIds = DistributedMapsFactory.getIdentifiersSet();
373 7806 tao
        Date otherPreviousDate = null;
374
        Date resourceMapPreviousDate = null;
375 7793 tao
        if(metacatIds != null) {
376
            for(Identifier identifier : metacatIds) {
377
                if(identifier != null && identifier.getValue() != null && !identifier.getValue().equals("")) {
378
                    SystemMetadata sysmeta = getSystemMetadata(identifier.getValue());
379
                    if(sysmeta != null && !sysmeta.getArchived()) {
380
                        ObjectFormatIdentifier formatId =sysmeta.getFormatId();
381
                        //System.out.println("the object format id is "+formatId.getValue());
382
                        //System.out.println("the ============ resourcMapNamespaces"+resourceMapNamespaces);
383
                        boolean correctTimeRange = false;
384
                        Date sysDate = sysmeta.getDateSysMetadataModified();
385
                        if(since == null && until == null) {
386
                            correctTimeRange = true;
387
                        } else if (since != null && until == null) {
388
                            if(sysDate.getTime() >= since.getTime()) {
389
                                correctTimeRange = true;
390
                            }
391
                        } else if (since == null && until != null) {
392
                            if(sysDate.getTime() <= until.getTime()) {
393
                                correctTimeRange = true;
394
                            }
395
                        } else if (since != null && until != null) {
396
                            if(sysDate.getTime() >= since.getTime() && sysDate.getTime() <= until.getTime()) {
397
                                correctTimeRange = true;
398
                            }
399
                        }
400
                        if(correctTimeRange && formatId != null && formatId.getValue() != null && resourceMapNamespaces != null && isResourceMap(formatId)) {
401 7806 tao
                            //for the resource map
402
                            if(!resourceMapIds.isEmpty()) {
403
                                if(sysDate.getTime() > resourceMapPreviousDate.getTime()) {
404
                                    resourceMapIds.add(identifier.getValue());//append to the end of the list if current is later than the previous one
405
                                } else {
406
                                    int size = resourceMapIds.size();//
407
                                    resourceMapIds.add(size -1, identifier.getValue());//keep the previous one at the end of the list.
408
                                }
409
                            } else {
410
                                resourceMapIds.add(identifier.getValue());
411
                            }
412
                            resourceMapPreviousDate = sysDate;
413 7793 tao
                        } else {
414 7806 tao
                            if(!otherIds.isEmpty()) {
415
                                if(sysDate.getTime() > otherPreviousDate.getTime()) {
416
                                    otherIds.add(identifier.getValue());
417 7740 tao
                                } else {
418 7806 tao
                                    int size = otherIds.size();
419
                                    otherIds.add(size-1, identifier.getValue());
420 7740 tao
                                }
421 7806 tao
                            } else {
422
                                otherIds.add(identifier.getValue());
423 7740 tao
                            }
424 7806 tao
                            otherPreviousDate = sysDate;
425 7607 tao
                        }
426 7806 tao
427 7607 tao
                    }
428
                }
429
            }
430 7806 tao
        }
431 7606 tao
        return ids;
432
    }
433
434
    /*
435 7740 tao
     * If the specified ObjectFormatIdentifier is a resrouce map namespace.
436
     */
437 7786 tao
    public static boolean isResourceMap(ObjectFormatIdentifier formatId) {
438 7740 tao
        boolean isResourceMap = false;
439
        if(formatId != null && resourceMapNamespaces != null) {
440
            for(String namespace : resourceMapNamespaces) {
441
                if(namespace != null && formatId.getValue() != null && !formatId.getValue().trim().equals("") && formatId.getValue().equals(namespace)) {
442
                    isResourceMap = true;
443
                    break;
444
                }
445
            }
446
        }
447
        return isResourceMap;
448
    }
449
450 7793 tao
451 7606 tao
452
    /*
453
     * Generate index for the id.
454
     */
455
    private void generateIndex(String id) throws Exception {
456
        if(id != null)  {
457 7691 tao
                SystemMetadata sysmeta = getSystemMetadata(id);
458
                //only update none-archived id.
459
                if(sysmeta != null && !sysmeta.getArchived()) {
460
                        InputStream data = getDataObject(id);
461 7606 tao
                        Identifier obsolete = sysmeta.getObsoletes();
462 7627 tao
                        List<String> obsoleteChain = null;
463 7606 tao
                        if(obsolete != null) {
464 7691 tao
                            obsoleteChain = getObsoletes(id);
465 7627 tao
                        }
466
                        solrIndex.update(id, obsoleteChain, sysmeta, data);
467 7606 tao
                } else {
468
                    throw new Exception("IndexGenerator.generate - there is no found SystemMetadata associated with the id "+id);
469
                }
470
471
        }
472
    }
473 7691 tao
474
    /*
475
     * Initialize the system metadata map
476
     */
477
    private void initSystemMetadataMap() throws FileNotFoundException, ServiceFailure{
478
        int times = 0;
479
        if(systemMetadataMap == null) {
480 7774 tao
            systemMetadataMap = DistributedMapsFactory.getSystemMetadataMap();
481
            /*while(true) {
482 7691 tao
                try {
483
                    systemMetadataMap = DistributedMapsFactory.getSystemMetadataMap();
484
                    break;
485
                } catch (FileNotFoundException e) {
486
                    throw e;
487
                } catch (ServiceFailure e) {
488 7748 tao
                    if(times <= maxAttempts) {
489 7691 tao
                        log.warn("IndexGenerator.initSystemMetadataMap - the hazelcast service is not ready : "
490 7748 tao
                                         +e.getMessage()+"\nWe will try to access it "+waitingTime/1000+" seconds later ");
491 7691 tao
                        try {
492 7748 tao
                            Thread.sleep(waitingTime);
493 7691 tao
                        } catch (Exception ee) {
494 7748 tao
                            log.warn("IndexGenerator.initSystemMetadataMap - the thread can't sleep for "+waitingTime/1000+" seconds to wait the hazelcast service");
495 7691 tao
                        }
496
497
                    } else {
498 7748 tao
                        throw new ServiceFailure("0000", "IndexGenerator.initSystemMetadataMap - the hazelcast service is not ready even though Metacat-index wailted for "+maxAttempts*waitingTime/1000+" seconds. We can't get the system metadata from it and the building index can't happen this time");
499 7691 tao
                    }
500
                }
501
                times++;
502 7774 tao
            }*/
503 7691 tao
        }
504
    }
505
506
    /*
507
     * We should call this method after calling initSystemMetadataMap since this method doesn't have the mechanism to wait the readiness of the hazelcast service
508
     */
509
    private void initObjectPathMap() throws FileNotFoundException, ServiceFailure {
510
        if(objectPathMap == null) {
511
            objectPathMap = DistributedMapsFactory.getObjectPathMap();
512
        }
513
    }
514
    /**
515
     * Get an InputStream as the data object for the specific pid.
516
     * @param pid
517
     * @return
518
     * @throws FileNotFoundException
519
     */
520
    private InputStream getDataObject(String pid) throws FileNotFoundException {
521
        Identifier identifier = new Identifier();
522
        identifier.setValue(pid);
523
        String objectPath = objectPathMap.get(identifier);
524
        InputStream data = null;
525
        data = new FileInputStream(objectPath);
526
        return data;
527
528
    }
529
530
    /**
531
     * Get the SystemMetadata for the specified id from the distributed Map.
532
     * The null maybe is returned if there is no system metadata found.
533
     * @param id  the specified id.
534
     * @return the SystemMetadata associated with the id.
535
     */
536
    private SystemMetadata getSystemMetadata(String id) {
537
        SystemMetadata metadata = null;
538
        if(systemMetadataMap != null && id != null) {
539
            Identifier identifier = new Identifier();
540
            identifier.setValue(id);
541
            metadata = systemMetadataMap.get(identifier);
542
        }
543
        return metadata;
544
    }
545
546
    /**
547
     * Get the obsoletes chain of the specified id. The returned list doesn't include
548
     * the specified id itself. The newer version has the lower index number in the list.
549
     * Empty list will be returned if there is no document to be obsoleted by this id.
550
     * @param id
551
     * @return
552
     */
553
    private List<String> getObsoletes(String id) {
554
        List<String> obsoletes = new ArrayList<String>();
555
        while (id != null) {
556
            SystemMetadata metadata = getSystemMetadata(id);
557
            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.
558
            if(metadata != null) {
559
                Identifier identifier = metadata.getObsoletes();
560
                if(identifier != null && identifier.getValue() != null && !identifier.getValue().trim().equals("")) {
561
                    obsoletes.add(identifier.getValue());
562
                    id = identifier.getValue();
563
                }
564
            }
565
        }
566
        return obsoletes;
567
    }
568 7788 tao
569
    /**
570
     * Overwrite and do nothing
571
     */
572
    public boolean cancel() {
573
        return true;
574
    }
575 7691 tao
576 7606 tao
}