Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author: $'
7
 *     '$Date: 2009-06-13 15:28:13 +0300  $'
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
 */
23
package edu.ucsb.nceas.metacat.dataone;
24

    
25
import java.io.ByteArrayOutputStream;
26
import java.io.File;
27
import java.io.FileInputStream;
28
import java.io.FileNotFoundException;
29
import java.io.FileOutputStream;
30
import java.io.IOException;
31
import java.io.InputStream;
32
import java.io.OutputStream;
33
import java.io.PrintWriter;
34
import java.io.StringBufferInputStream;
35
import java.security.MessageDigest;
36
import java.sql.SQLException;
37
import java.util.*;
38
import java.text.DateFormat;
39
import java.text.SimpleDateFormat;
40

    
41
import javax.servlet.ServletContext;
42
import javax.servlet.http.HttpServletRequest;
43
import javax.servlet.http.HttpServletResponse;
44

    
45
import org.apache.commons.io.IOUtils;
46
import org.apache.log4j.Logger;
47
import org.dataone.service.exceptions.IdentifierNotUnique;
48
import org.dataone.service.exceptions.InsufficientResources;
49
import org.dataone.service.exceptions.InvalidRequest;
50
import org.dataone.service.exceptions.InvalidSystemMetadata;
51
import org.dataone.service.exceptions.InvalidToken;
52
import org.dataone.service.exceptions.NotAuthorized;
53
import org.dataone.service.exceptions.NotFound;
54
import org.dataone.service.exceptions.NotImplemented;
55
import org.dataone.service.exceptions.ServiceFailure;
56
import org.dataone.service.exceptions.UnsupportedType;
57
import org.dataone.service.mn.MemberNodeCrud;
58
import org.dataone.service.types.*;
59
import org.jibx.runtime.BindingDirectory;
60
import org.jibx.runtime.IBindingFactory;
61
import org.jibx.runtime.IMarshallingContext;
62
import org.jibx.runtime.IUnmarshallingContext;
63
import org.jibx.runtime.JiBXException;
64

    
65
import org.dataone.service.types.Identifier;
66
import org.dataone.service.types.util.ServiceTypeUtil;
67

    
68
import com.gc.iotools.stream.is.InputStreamFromOutputStream;
69

    
70
import edu.ucsb.nceas.metacat.AccessionNumber;
71
import edu.ucsb.nceas.metacat.AccessionNumberException;
72
import edu.ucsb.nceas.metacat.MetacatResultSet;
73
import edu.ucsb.nceas.metacat.MetacatResultSet.Document;
74
import edu.ucsb.nceas.metacat.DBQuery;
75
import edu.ucsb.nceas.metacat.DocumentImpl;
76
import edu.ucsb.nceas.metacat.EventLog;
77
import edu.ucsb.nceas.metacat.IdentifierManager;
78
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
79
import edu.ucsb.nceas.metacat.McdbException;
80
import edu.ucsb.nceas.metacat.MetacatHandler;
81
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
82
import edu.ucsb.nceas.metacat.client.rest.MetacatRestClient;
83
import edu.ucsb.nceas.metacat.properties.PropertyService;
84
import edu.ucsb.nceas.metacat.replication.ForceReplicationHandler;
85
import edu.ucsb.nceas.metacat.service.SessionService;
86
import edu.ucsb.nceas.metacat.util.DocumentUtil;
87
import edu.ucsb.nceas.metacat.util.SessionData;
88
import edu.ucsb.nceas.utilities.ParseLSIDException;
89
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
90

    
91
/**
92
 * 
93
 * Implements DataONE MemberNode CRUD API for Metacat. 
94
 * 
95
 * @author Matthew Jones
96
 */
97
public class CrudService implements MemberNodeCrud
98
{
99
    private static CrudService crudService = null;
100

    
101
    private MetacatHandler handler;
102
    private Hashtable<String, String[]> params;
103
    private Logger logMetacat = null;
104
    private Logger logCrud = null;
105
    
106
    private String metacatUrl;
107
    
108
    private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
109

    
110
    /**
111
     * singleton accessor
112
     */
113
    public static CrudService getInstance() 
114
    {
115
      if(crudService == null)
116
      {
117
        crudService = new CrudService();
118
      }
119
      
120
      return crudService;
121
    }
122
    
123
    /**
124
     * Constructor, private for singleton access
125
     */
126
    private CrudService() {
127
        logMetacat = Logger.getLogger(CrudService.class);
128
        logCrud = Logger.getLogger("DataOneLogger");
129
        try
130
        {
131
            String server = PropertyService.getProperty("server.name");
132
            String port = PropertyService.getProperty("server.httpPort");
133
            String context = PropertyService.getProperty("application.context");
134
            metacatUrl = "http://" + server + ":" + port + "/" + context + "/d1";
135
            logMetacat.debug("Initializing CrudService with url " + metacatUrl);
136
        }
137
        catch(Exception e)
138
        {
139
            logMetacat.error("Could not find servlet url in CrudService: " + e.getMessage());
140
            e.printStackTrace();
141
            throw new RuntimeException("Error getting servlet url in CrudService: " + e.getMessage());
142
        }
143
        
144
        params = new Hashtable<String, String[]>();
145
        handler = new MetacatHandler(new Timer());
146
    }
147
    
148
    /**
149
     * return the context url CrudService is using.
150
     */
151
    public String getContextUrl()
152
    {
153
        return metacatUrl;
154
    }
155
    
156
    /**
157
     * Set the context url that this service uses.  It is normally not necessary
158
     * to call this method unless you are trying to connect to a server other
159
     * than the one in which this service is installed.  Otherwise, this value is
160
     * taken from the metacat.properties file (server.name, server.port, application.context).
161
     */
162
    public void setContextUrl(String url)
163
    {
164
        metacatUrl = url;
165
    }
166
    
167
    /**
168
     * set the params for this service from an HttpServletRequest param list
169
     */
170
    public void setParamsFromRequest(HttpServletRequest request)
171
    {
172
        Enumeration paramlist = request.getParameterNames();
173
        while (paramlist.hasMoreElements()) {
174
            String name = (String) paramlist.nextElement();
175
            String[] value = (String[])request.getParameterValues(name);
176
            params.put(name, value);
177
        }
178
    }
179
    
180
    /**
181
     * Authenticate against metacat and get a token.
182
     * @param username
183
     * @param password
184
     * @return
185
     * @throws ServiceFailure
186
     */
187
    public AuthToken authenticate(String username, String password)
188
      throws ServiceFailure
189
    {
190
        /* TODO:
191
         * This method is not in the original D1 crud spec.  It is highly
192
         * metacat centric.  Higher level decisions need to be made on authentication
193
         * interfaces for D1 nodes.
194
         */
195
        try
196
        {
197
            MetacatRestClient restClient = new MetacatRestClient(getContextUrl());   
198
            String response = restClient.login(username, password);
199
            String sessionid = restClient.getSessionId();
200
            SessionService sessionService = SessionService.getInstance();
201
            sessionService.registerSession(new SessionData(sessionid, username, new String[0], password, "CrudServiceLogin"));
202
            AuthToken token = new AuthToken(sessionid);
203
            EventLog.getInstance().log(metacatUrl,
204
                    username, null, "authenticate");
205
            logCrud.info("authenticate");
206
            return token;
207
        }
208
        catch(Exception e)
209
        {
210
            throw new ServiceFailure("1620", "Error authenticating with metacat: " + e.getMessage());
211
        }
212
    }
213
    
214
    /**
215
     * set the parameter values needed for this request
216
     */
217
    public void setParameter(String name, String[] value)
218
    {
219
        params.put(name, value);
220
    }
221
    
222
    /**
223
     * Generate SystemMetadata for any object in the object store that does
224
     * not already have it.  SystemMetadata documents themselves, are, of course,
225
     * exempt.  This is a utility method for migration of existing object 
226
     * stores to DataONE where SystemMetadata is required for all objects.  See 
227
     * https://trac.dataone.org/ticket/591
228
     * 
229
     * @param token an authtoken with appropriate permissions to read all 
230
     * documents in the object store.  To work correctly, this should probably
231
     * be an adminstrative credential.
232
     */
233
    public void generateMissingSystemMetadata(AuthToken token)
234
    {
235
        IdentifierManager im = IdentifierManager.getInstance();
236
        //get the list of ids with no SM
237
        List<String> l = im.getLocalIdsWithNoSystemMetadata();
238
        for(int i=0; i<l.size(); i++)
239
        { //for each id, add a system metadata doc
240
            String localId = l.get(i);
241
            System.out.println("Creating SystemMetadata for localId " + localId);
242
            //get the document
243
            try
244
            {
245
                //generate required system metadata fields from the document
246
                SystemMetadata sm = createSystemMetadata(localId, token);
247
                //insert the systemmetadata object
248
                SessionData sessionData = getSessionData(token);
249
                String smlocalid = insertSystemMetadata(sm, sessionData);
250
                System.out.println("setting access on SM doc with localid " + smlocalid);
251
                handler.setAccess(metacatUrl, sessionData.getUserName(), smlocalid,
252
                        "public", "4", "allow", "allowFirst");
253
                
254
                String username = "public";
255
                if(sessionData != null)
256
                {
257
                    username = sessionData.getUserName();
258
                }
259
                EventLog.getInstance().log(metacatUrl,
260
                        username, localId, "generateMissingSystemMetadata");
261
            }
262
            catch(Exception e)
263
            {
264
                e.printStackTrace();
265
                System.out.println("Exception generating missing system metadata: " + e.getMessage());
266
                logMetacat.error("Could not generate missing system metadata: " + e.getMessage());
267
            }
268
        }
269
        logCrud.info("generateMissingSystemMetadata");
270
    }
271
    
272
    /**
273
     * create an object via the crud interface
274
     */
275
    public Identifier create(AuthToken token, Identifier guid, 
276
            InputStream object, SystemMetadata sysmeta) throws InvalidToken, 
277
            ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType, 
278
            InsufficientResources, InvalidSystemMetadata, NotImplemented {
279
        logMetacat.debug("Starting CrudService.create()...");
280
        
281
        // authenticate & get user info
282
        SessionData sessionData = getSessionData(token);
283
        String username = "public";
284
        String[] groups = null;
285
        if(sessionData != null)
286
        {
287
            username = sessionData.getUserName();
288
            groups = sessionData.getGroupNames();
289
        }
290
        String localId = null;
291

    
292
        if (username == null || username.equals("public"))
293
        {
294
            //TODO: many of the thrown exceptions do not use the correct error codes
295
            //check these against the docs and correct them
296
            throw new NotAuthorized("1100", "User " + username + " is not authorized to create content." +
297
                    "  If you are not logged in, please do so and retry the request.");
298
        }
299
        
300
        // verify that guid == SystemMetadata.getIdentifier()
301
        logMetacat.debug("Comparing guid|sysmeta_guid: " + guid.getValue() + "|" + sysmeta.getIdentifier().getValue());
302
        if (!guid.getValue().equals(sysmeta.getIdentifier().getValue())) {
303
            throw new InvalidSystemMetadata("1180", 
304
                "GUID in method call (" + guid.getValue() + ") does not match GUID in system metadata (" +
305
                sysmeta.getIdentifier().getValue() + ").");
306
        }
307

    
308
        logMetacat.debug("Checking if identifier exists...");
309
        // Check that the identifier does not already exist
310
        IdentifierManager im = IdentifierManager.getInstance();
311
        if (im.identifierExists(guid.getValue())) {
312
            throw new IdentifierNotUnique("1120", 
313
                "GUID is already in use by an existing object.");
314
        }
315

    
316
        // Check if we are handling metadata or data
317
        boolean isScienceMetadata = isScienceMetadata(sysmeta);
318
        
319
        if (isScienceMetadata) {
320
            // CASE METADATA:
321
            try {
322
                //System.out.println("CrudService: inserting document with guid " + guid.getValue());
323
                this.insertDocument(object, guid, sessionData);
324
                localId = im.getLocalId(guid.getValue());
325
            } catch (IOException e) {
326
                String msg = "Could not create string from XML stream: " +
327
                    " " + e.getMessage();
328
                logMetacat.debug(msg);
329
                throw new ServiceFailure("1190", msg);
330
            } catch(Exception e) {
331
                String msg = "Unexpected error in CrudService.create: " + e.getMessage();
332
                logMetacat.debug(msg);
333
                throw new ServiceFailure("1190", msg);
334
            }
335
            
336

    
337
        } else {
338
            // DEFAULT CASE: DATA (needs to be checked and completed)
339
            localId = insertDataObject(object, guid, sessionData);
340
            
341
        }
342

    
343
        // For Metadata and Data, insert the system metadata into the object store too
344
        String sysMetaLocalId = insertSystemMetadata(sysmeta, sessionData);
345
        //get the document info.  add any access params for the sysmeta too
346
        //System.out.println("looking for access records to add for system " +
347
        //    "metadata who's parent doc's  local id is " + localId);
348
        try
349
        {
350
            Hashtable<String, Object> h = im.getDocumentInfo(localId.substring(0, localId.lastIndexOf(".")));
351
            Vector v = (Vector)h.get("access");
352
            for(int i=0; i<v.size(); i++)
353
            {
354
                Hashtable ah = (Hashtable)v.elementAt(i);
355
                String principal = (String)ah.get("principal_name");
356
                String permission = (String)ah.get("permission");
357
                String permissionType = (String)ah.get("permission_type");
358
                String permissionOrder = (String)ah.get("permission_order");
359
                int perm = new Integer(permission).intValue();
360
                //System.out.println("found access record for principal " + principal);
361
                //System.out.println("permission: " + perm + " perm_type: " + permissionType + 
362
                //    " perm_order: " + permissionOrder);
363
                this.setAccess(token, guid, principal, perm, permissionType, permissionOrder, true);
364
            }
365
        }
366
        catch(Exception e)
367
        {
368
            logMetacat.error("Error setting permissions on System Metadata object " + 
369
                    " with id " + sysMetaLocalId + ": " + e.getMessage());
370
            //TODO: decide if this error should cancel the entire create or
371
            //if it should continue with just a logged error.
372
        }
373
        
374
        
375
        logMetacat.debug("Returning from CrudService.create()");
376
        EventLog.getInstance().log(metacatUrl,
377
                username, localId, "create");
378
        logCrud.info("create D1GUID:" + guid.getValue() + ":D1SCIMETADATA:" + localId + 
379
                ":D1SYSMETADATA:"+ sysMetaLocalId + ":");
380
        return guid;
381
    }
382
    
383
    /**
384
     * update an existing object with a new object.  Change the system metadata
385
     * to reflect the changes and update it as well.
386
     */
387
    public Identifier update(AuthToken token, Identifier guid, 
388
            InputStream object, Identifier obsoletedGuid, SystemMetadata sysmeta) 
389
            throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, 
390
            UnsupportedType, InsufficientResources, NotFound, InvalidSystemMetadata, 
391
            NotImplemented {
392
        try
393
        {
394
            SessionData sessionData = getSessionData(token);
395
            
396
            //find the old systemmetadata (sm.old) document id (the one linked to obsoletedGuid)
397
            SystemMetadata sm = getSystemMetadata(token, obsoletedGuid);
398
            //change sm.old's obsoletedBy field 
399
            List l = sm.getObsoletedByList();
400
            l.add(guid);
401
            sm.setObsoletedByList(l);
402
            //update sm.old
403
            updateSystemMetadata(sm, sessionData);
404
            
405
            //change the obsoletes field of the new systemMetadata (sm.new) to point to the id of the old one
406
            sysmeta.addObsolete(obsoletedGuid);
407
            //insert sm.new
408
            String sysMetaLocalId = insertSystemMetadata(sysmeta, sessionData);
409
            String localId;
410
            
411
            boolean isScienceMetadata = isScienceMetadata(sysmeta);
412
            if(isScienceMetadata)
413
            {
414
                //update the doc
415
                localId = updateDocument(object, obsoletedGuid, guid, sessionData, false);
416
            }
417
            else
418
            {
419
                //update a data file, not xml
420
                localId = insertDataObject(object, guid, sessionData);
421
            }
422
            
423
            IdentifierManager im = IdentifierManager.getInstance();
424
            String username = "public";
425
            if(sessionData != null)
426
            {
427
                username = sessionData.getUserName();
428
            }
429
            EventLog.getInstance().log(metacatUrl,
430
                    username, im.getLocalId(guid.getValue()), "update");
431
            logCrud.info("update D1GUID:" + guid.getValue() + ":D1SCIMETADATA:" + localId + 
432
                    ":D1SYSMETADATA:"+ sysMetaLocalId + ":");
433
            return guid;
434
        }
435
        catch(Exception e)
436
        {
437
            throw new ServiceFailure("1310", "Error updating document in CrudService: " + e.getMessage());
438
        }
439
    }
440
    
441
    /**
442
     * set access permissions on both the science metadata and system metadata
443
     */
444
    public void setAccess(AuthToken token, Identifier id, String principal, String permission,
445
            String permissionType, String permissionOrder)
446
      throws ServiceFailure
447
    {
448
        setAccess(token, id, principal, permission, permissionType, permissionOrder, true);
449
    }
450
    
451
    /**
452
     * set access control on the doc
453
     * @param token
454
     * @param id
455
     * @param principal
456
     * @param permission
457
     */
458
    public void setAccess(AuthToken token, Identifier id, String principal, int permission,
459
      String permissionType, String permissionOrder, boolean setSystemMetadata)
460
      throws ServiceFailure
461
    {
462
        String perm = "";
463
        if(permission >= 4)
464
        {
465
            perm = "read";
466
        }
467
        if(permission >= 6)
468
        {
469
            perm = "write";
470
        }
471
        //System.out.println("perm in setAccess: " + perm);
472
        //System.out.println("permission in setAccess: " + permission);
473
        setAccess(token, id, principal, perm, permissionType, permissionOrder,
474
                setSystemMetadata);
475
       
476
    }
477
    
478
    /**
479
     * set the permission on the document
480
     * @param token
481
     * @param principal
482
     * @param permission
483
     * @param permissionType
484
     * @param permissionOrder
485
     * @return
486
     */
487
    public void setAccess(AuthToken token, Identifier id, String principal, String permission,
488
            String permissionType, String permissionOrder, boolean setSystemMetadata)
489
      throws ServiceFailure
490
    {
491
        /* TODO:
492
         * This is also not part of the D1 Crud spec.  This method is needed for
493
         * systems such as metacat where access to objects is controlled by
494
         * and ACL.  Higher level decisions need to be made about how this
495
         * should work within D1.
496
         */
497
        try
498
        {
499
            final SessionData sessionData = getSessionData(token);
500
            if(sessionData == null)
501
            {
502
                throw new ServiceFailure("1000", "User must be logged in to set access.");
503
            }
504
            IdentifierManager im = IdentifierManager.getInstance();
505
            String docid = im.getLocalId(id.getValue());
506
        
507
            String permNum = "0";
508
            if(permission.equals("read"))
509
            {
510
                permNum = "4";
511
            }
512
            else if(permission.equals("write"))
513
            {
514
                permNum = "6";
515
            }
516
            System.out.println("user " + sessionData.getUserName() + 
517
                    " is setting access level " + permNum + " for permission " + 
518
                    permissionType + " on doc with localid " + docid);
519
            handler.setAccess(metacatUrl, sessionData.getUserName(), docid, 
520
                    principal, permNum, permissionType, permissionOrder);
521
            if(setSystemMetadata)
522
            {
523
                //set the same perms on the system metadata doc
524
                String smlocalid = im.getSystemMetadataLocalId(id.getValue());
525
                System.out.println("setting access on SM doc with localid " + smlocalid);
526
                //cs.setAccess(token, smid, principal, permission, permissionType, permissionOrder);
527
                handler.setAccess(metacatUrl, sessionData.getUserName(), smlocalid,
528
                        principal, permNum, permissionType, permissionOrder);
529
            }
530
            String username = "public";
531
            if(sessionData != null)
532
            {
533
                username = sessionData.getUserName();
534
            }
535
            EventLog.getInstance().log(metacatUrl,
536
                    username, im.getLocalId(id.getValue()), "setAccess");
537
            logCrud.info("setAccess");
538
        }
539
        catch(Exception e)
540
        {
541
            e.printStackTrace();
542
            throw new ServiceFailure("1000", "Could not set access on the document with id " + id.getValue());
543
        }
544
    }
545
    
546
    /**
547
     *  Retrieve the list of objects present on the MN that match the calling 
548
     *  parameters. This method is required to support the process of Member 
549
     *  Node synchronization. At a minimum, this method should be able to 
550
     *  return a list of objects that match:
551
     *  startTime <= SystemMetadata.dateSysMetadataModified
552
     *  but is expected to also support date range (by also specifying endTime), 
553
     *  and should also support slicing of the matching set of records by 
554
     *  indicating the starting index of the response (where 0 is the index 
555
     *  of the first item) and the count of elements to be returned.
556
     *  
557
     *  If startTime or endTime is null, the query is not restricted by that parameter.
558
     *  
559
     * @see http://mule1.dataone.org/ArchitectureDocs/mn_api_replication.html#MN_replication.listObjects
560
     * @param token
561
     * @param startTime
562
     * @param endTime
563
     * @param objectFormat
564
     * @param replicaStatus
565
     * @param start
566
     * @param count
567
     * @return ObjectList
568
     * @throws NotAuthorized
569
     * @throws InvalidRequest
570
     * @throws NotImplemented
571
     * @throws ServiceFailure
572
     * @throws InvalidToken
573
     */
574
    public ObjectList listObjects(AuthToken token, Date startTime, Date endTime, 
575
        ObjectFormat objectFormat, boolean replicaStatus, int start, int count)
576
      throws NotAuthorized, InvalidRequest, NotImplemented, ServiceFailure, InvalidToken
577
    {
578
      ObjectList ol = new ObjectList();
579
      final SessionData sessionData = getSessionData(token);
580
      int totalAfterQuery = 0;
581
      
582
      try
583
      {
584
          if (PropertyService.getProperty("database.queryCacheOn").equals("true"))
585
          {
586
              //System.out.println("the string stored into cache is "+ resultsetBuffer.toString());
587
              DBQuery.clearQueryResultCache();
588
          }
589
      }
590
      catch (PropertyNotFoundException e1)
591
      {
592
          //just don't do anything
593
      }
594
      
595
      try
596
      {
597
          //TODO: Milliseconds need to be added to the dateFormat
598
          System.out.println("=========== Listing Objects =============");
599
          System.out.println("Current server time is: " + new Date());
600
          if(startTime != null)
601
          {
602
              System.out.println("query start time is " + startTime);
603
          }
604
          if(endTime != null)
605
          {
606
              System.out.println("query end time is " + endTime);
607
          }
608
          params.clear();
609
          params.put("qformat", new String[] {PropertyService.getProperty("crudService.listObjects.QFormat")});
610
          params.put("action", new String[] {"squery"});
611
          params.put("query", new String[] {createListObjectsPathQueryDocument()});
612
          
613
          /*System.out.println("query is: metacatUrl: " + metacatUrl + " user: " + sessionData.getUserName() +
614
                  " sessionid: " + sessionData.getId() + " params: ");
615
          String url = metacatUrl + "/metacat?action=query&sessionid=" + sessionData.getId();
616
          Enumeration keys = params.keys();
617
          while(keys.hasMoreElements())
618
          {
619
              String key = (String)keys.nextElement();
620
              String[] parr = params.get(key);
621
              for(int i=0; i<parr.length; i++)
622
              {
623
                  System.out.println("param " + key + ": " + parr[i]);
624
                  url += "&" + key + "=" + parr[i] ;
625
              }
626
          }
627
          System.out.println("query url: " + url);
628
          */
629
          String username = "public";
630
          String[] groups = null;
631
          String sessionid = "";
632
          if(sessionData != null)
633
          {
634
              username = sessionData.getUserName();
635
              groups = sessionData.getGroupNames();
636
              sessionid = sessionData.getId();
637
          }
638
          
639
          MetacatResultSet rs = handler.query(metacatUrl, params, username, 
640
                  groups, sessionid);
641
          List docs = rs.getDocuments();
642
          
643
          System.out.println("query returned " + docs.size() + " documents.");
644
          Vector<Document> docCopy = new Vector<Document>();
645
          
646
          //preparse the list to remove any that don't match the query params
647
          /* TODO: this type of query/subquery processing is probably not scalable
648
           * to larger object stores.  This code should be revisited.  The metacat
649
           * query handler should probably be altered to handle the type of query 
650
           * done here.
651
           */ 
652
          for(int i=0; i<docs.size(); i++)
653
          {
654
              Document d = (Document)docs.get(i);
655
              
656
              ObjectFormat returnedObjectFormat = ObjectFormat.convert(d.getField("objectFormat"));
657
              
658
              if(returnedObjectFormat != null && 
659
                 objectFormat != null && 
660
                 !objectFormat.toString().trim().equals(returnedObjectFormat.toString().trim()))
661
              { //make sure the objectFormat is the one specified
662
                  continue;
663
              }
664
              
665
              String dateSMM = d.getField("dateSysMetadataModified");
666
              if((startTime != null || endTime != null) && dateSMM == null)
667
              {  //if startTime or endTime are not null, we need a date to compare to
668
                  continue;
669
              }
670
              
671
              //date parse
672
              Date dateSysMetadataModified = null;
673
              if(dateSMM != null)
674
              {
675

    
676
                  /*                  
677
                  if(dateSMM.indexOf(".") != -1)
678
                  {  //strip the milliseconds
679
                      //TODO: don't do this. we need milliseconds now.
680
                      //TODO: explore ISO 8601 to figure out milliseconds
681
                      dateSMM = dateSMM.substring(0, dateSMM.indexOf(".")) + 'Z';
682
                  }
683
                  */
684
                  //System.out.println("dateSMM: " + dateSMM);
685
                  //dateFormat.setTimeZone(TimeZone.getTimeZone("GMT-0"));
686
                  try
687
                  {   //the format we want
688
                      dateSysMetadataModified = dateFormat.parse(dateSMM);
689
                  }
690
                  catch(java.text.ParseException pe)
691
                  {   //try another legacy format
692
                      try
693
                      {
694
                          DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.S'Z'");
695
                          dateFormat2.setTimeZone(TimeZone.getTimeZone("GMT-0"));
696
                          dateSysMetadataModified = dateFormat2.parse(dateSMM);
697
                      }
698
                      catch(java.text.ParseException pe2)
699
                      {
700
                          //try another legacy format
701
                          DateFormat dateFormat3 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
702
                          dateFormat3.setTimeZone(TimeZone.getTimeZone("GMT-0"));
703
                          dateSysMetadataModified = dateFormat3.parse(dateSMM);
704
                      }
705
                      
706
                  }                  
707
              }
708
              
709
              /*System.out.println("====================================");
710
              System.out.println("doc number " + i);
711
              System.out.println("docid: " + d.docid);
712
              System.out.println("guid: " + d.getField("identifier").trim());
713
              System.out.println("dateSMM: " + dateSMM);
714
              System.out.println("dateSysMetadataModified: " + dateSysMetadataModified);
715
              System.out.println("startTime: " + startTime);
716
              System.out.println("endtime: " + endTime);*/
717
              
718
              int startDateComparison = 0;
719
              int endDateComparison = 0;
720
              if(startTime != null)
721
              {
722
                  Calendar zTime = Calendar.getInstance(TimeZone.getTimeZone("GMT-0"));
723
                  zTime.setTime(startTime);
724
                  startTime = zTime.getTime();
725
                  
726
                  if(dateSysMetadataModified == null)
727
                  {
728
                      startDateComparison = -1;
729
                  }
730
                  else
731
                  {
732
                      startDateComparison = dateSysMetadataModified.compareTo(startTime);
733
                  }
734
                  //System.out.println("startDateCom: " + startDateComparison);
735
              }
736
              else
737
              {
738
                  startDateComparison = 1;
739
              }
740
              
741
              if(endTime != null)
742
              {
743
                  Calendar zTime = Calendar.getInstance(TimeZone.getTimeZone("GMT-0"));
744
                  zTime.setTime(endTime);
745
                  endTime = zTime.getTime();
746
                  
747
                  if(dateSysMetadataModified == null)
748
                  {
749
                      endDateComparison = 1;
750
                  }
751
                  else
752
                  {
753
                      endDateComparison = dateSysMetadataModified.compareTo(endTime);
754
                  }
755
                  //System.out.println("endDateCom: " + endDateComparison);
756
              }
757
              else
758
              {
759
                  endDateComparison = -1;
760
              }
761
              
762
              
763
              if(startDateComparison < 0 || endDateComparison > 0)
764
              { 
765
                  continue;                  
766
              }
767
              
768
              docCopy.add((Document)docs.get(i));
769
          } //end pre-parse
770
          
771
          docs = docCopy;
772
          totalAfterQuery = docs.size();
773
          //System.out.println("total after subquery: " + totalAfterQuery);
774
          
775
          //make sure we don't run over the end
776
          int end = start + count;
777
          if(end > docs.size())
778
          {
779
              end = docs.size();
780
          }
781
          
782
          for(int i=start; i<end; i++)
783
          {
784
              //get the document from the result
785
              Document d = (Document)docs.get(i);
786
              //System.out.println("processing doc " + d.docid);
787
              
788
              String dateSMM = d.getField("dateSysMetadataModified");
789
              //System.out.println("dateSMM: " + dateSMM);
790
              //System.out.println("parsed date: " + parseDate(dateSMM));
791
              Date dateSysMetadataModified = null;
792
              if(dateSMM != null)
793
              {
794
                  try
795
                  {
796
                      dateSysMetadataModified = parseDate(dateSMM);
797
                  }
798
                  catch(Exception e)
799
                  { //if we fail to parse the date, just ignore the value
800
                      dateSysMetadataModified = null;
801
                  }
802
              }
803
              ObjectFormat returnedObjectFormat = ObjectFormat.convert(d.getField("objectFormat"));
804
                
805
              
806
              ObjectInfo info = new ObjectInfo();
807
              //add the fields to the info object
808
              Checksum cs = new Checksum();
809
              cs.setValue(d.getField("checksum"));
810
              String csalg = d.getField("algorithm");
811
              if(csalg == null)
812
              {
813
                  csalg = "MD5";
814
              }
815
              ChecksumAlgorithm ca = ChecksumAlgorithm.convert(csalg);
816
              cs.setAlgorithm(ca);
817
              info.setChecksum(cs);
818
              info.setDateSysMetadataModified(dateSysMetadataModified);
819
              Identifier id = new Identifier();
820
              id.setValue(d.getField("identifier").trim());
821
              info.setIdentifier(id);
822
              info.setObjectFormat(returnedObjectFormat);
823
              String size = d.getField("size");
824
              if(size != null)
825
              {
826
                  info.setSize(new Long(size.trim()).longValue());
827
              }
828
              //add the ObjectInfo to the ObjectList
829
              //logCrud.info("objectFormat: " + info.getObjectFormat().toString());
830
              //logCrud.info("id: " + info.getIdentifier().getValue());
831
              
832
              if(info.getIdentifier().getValue() != null)
833
              { //id can be null from tests.  should not happen in production.
834
                  if((info.getObjectFormat() != null && !info.getObjectFormat().toString().trim().equals("")))
835
                  { //objectFormat needs to not be null and not be an empty string
836
                    ol.addObjectInfo(info);
837
                  }
838
                  else
839
                  {
840
                      logCrud.info("Not adding object with null objectFormat" + info.getIdentifier().getValue().toString());
841
                  }
842
              }
843
             
844
          }
845
      }
846
      catch(Exception e)
847
      {
848
          e.printStackTrace();
849
          logCrud.error("Error creating ObjectList: " + e.getMessage() + " cause: " + e.getCause());
850
          throw new ServiceFailure("1580", "Error retrieving ObjectList: " + e.getMessage());
851
      }
852
      String username = "public";
853
      if(sessionData != null)
854
      {
855
          username = sessionData.getUserName();
856
      }
857
      EventLog.getInstance().log(metacatUrl,
858
              username, null, "read");
859
      logCrud.info("listObjects");
860
      if(totalAfterQuery < count)
861
      {
862
          count = totalAfterQuery;
863
      }
864
      ol.setCount(count);
865
      ol.setStart(start);
866
      ol.setTotal(totalAfterQuery);
867
      return ol;
868
    }
869
    
870
    /**
871
     * Call listObjects with the default values for replicaStatus (true), start (0),
872
     * and count (1000).
873
     * @param token
874
     * @param startTime
875
     * @param endTime
876
     * @param objectFormat
877
     * @return
878
     * @throws NotAuthorized
879
     * @throws InvalidRequest
880
     * @throws NotImplemented
881
     * @throws ServiceFailure
882
     * @throws InvalidToken
883
     */
884
    public ObjectList listObjects(AuthToken token, Date startTime, Date endTime, 
885
        ObjectFormat objectFormat)
886
      throws NotAuthorized, InvalidRequest, NotImplemented, ServiceFailure, InvalidToken
887
    {
888
       return listObjects(token, startTime, endTime, objectFormat, true, 0, 1000);
889
    }
890

    
891
    /**
892
     * Delete a document. 
893
     */
894
    public Identifier delete(AuthToken token, Identifier guid)
895
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
896
            NotImplemented, InvalidRequest {
897
        logCrud.info("delete");
898
        
899
        if(token == null || token.getToken().equals("publid"))
900
        {
901
            throw new NotAuthorized("1320", "You must be logged in to delete records.");
902
        }
903
        
904
        if(guid == null || guid.getValue().trim().equals(""))
905
        {
906
            throw new InvalidRequest("1322", "No GUID specified in CrudService.delete()");
907
        }
908
        final SessionData sessionData = getSessionData(token);
909
        IdentifierManager manager = IdentifierManager.getInstance();
910
        
911
        String docid;
912
        try
913
        {
914
            docid = manager.getLocalId(guid.getValue());
915
        }
916
        catch(McdbDocNotFoundException mnfe)
917
        {
918
            throw new InvalidRequest("1322", "GUID " + guid + " not found.");
919
        }
920
        
921
        try
922
        {
923
            DocumentImpl.delete(docid, sessionData.getUserName(), sessionData.getGroupNames(), null);
924
        }
925
        catch(Exception e)
926
        {
927
            throw new ServiceFailure("1350", "Could not delete document: " + e.getMessage());
928
        }
929
        
930
        return guid;
931
    }
932

    
933
    /**
934
     * describe a document.  
935
     */
936
    public DescribeResponse describe(AuthToken token, Identifier guid)
937
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
938
            NotImplemented, InvalidRequest {
939
        logCrud.info("describe");
940
        
941
        if(token == null)
942
        {
943
            throw new InvalidToken("1370", "Authentication token is null");
944
        }
945
        
946
        if(guid == null || guid.getValue().trim().equals(""))
947
        {
948
            throw new InvalidRequest("1362", "Guid is null.  A valid guid is required.");
949
        }
950
        
951
        SystemMetadata sm = getSystemMetadata(token, guid);
952
        DescribeResponse dr = new DescribeResponse(sm.getObjectFormat(), 
953
                sm.getSize(), sm.getDateSysMetadataModified(), sm.getChecksum());
954
        return dr;
955
    }
956
    
957
    /**
958
     * get a document with a specified guid.
959
     */
960
    public InputStream get(AuthToken token, Identifier guid)
961
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
962
            NotImplemented {
963
        
964
        // Retrieve the session information from the AuthToken
965
        // If the session is expired, then the user is 'public'
966
        if(token == null)
967
        {
968
            token = new AuthToken("Public");
969
        }
970
        final SessionData sessionData = getSessionData(token);
971
        
972
        // Look up the localId for this global identifier
973
        IdentifierManager im = IdentifierManager.getInstance();
974
        
975
        try 
976
        {
977
            final String localId = im.getLocalId(guid.getValue());
978
            InputStream objectStream;
979
            try 
980
            {
981
                String username = "public";
982
                String[] groups = new String[0];
983
                if(sessionData != null)
984
                {
985
                    username = sessionData.getUserName();
986
                    groups = sessionData.getGroupNames();
987
                }
988
                
989
                objectStream = readFromMetacat(localId, username, groups);
990
                
991
            } catch (PropertyNotFoundException e) {
992
                e.printStackTrace();
993
                throw new ServiceFailure("1030", "Error getting property from metacat: " + e.getMessage());
994
            } catch (ClassNotFoundException e) {
995
                e.printStackTrace();
996
                throw new ServiceFailure("1030", "Class not found error when reading from metacat: " + e.getMessage());
997
            } catch (IOException e) {
998
                e.printStackTrace();
999
                throw new ServiceFailure("1030", "IOException while reading from metacat: " + e.getMessage());
1000
            } catch (SQLException e) {
1001
                e.printStackTrace();
1002
                throw new ServiceFailure("1030", "SQLException while reading from metacat: " + e.getMessage());
1003
            } catch (McdbException e) {
1004
                e.printStackTrace();
1005
                throw new ServiceFailure("1030", "Metacat DB exception while reading from metacat: " + e.getMessage());
1006
            } catch (ParseLSIDException e) {
1007
                e.printStackTrace();
1008
                throw new NotFound("1020", "LSID parsing exception while reading from metacat: " + e.getMessage());
1009
            } catch (InsufficientKarmaException e) {
1010
                e.printStackTrace();
1011
                throw new NotAuthorized("1000", "User not authorized for get(): " + e.getMessage());
1012
            }
1013
        
1014
        
1015
            String username = "public";
1016
            if(sessionData != null)
1017
            {
1018
                username = sessionData.getUserName();
1019
            }
1020
            
1021
            EventLog.getInstance().log(metacatUrl,
1022
                    username, im.getLocalId(guid.getValue()), "read");
1023
            logCrud.info("get D1GUID:" + guid.getValue() + ":D1SCIMETADATA:" + localId + 
1024
                    ":");
1025
            
1026
            return objectStream;
1027
        } 
1028
        catch (McdbDocNotFoundException e) 
1029
        {
1030
            throw new NotFound("1020", e.getMessage());
1031
        } 
1032
    }
1033

    
1034
    /**
1035
     * get the checksum for a document.  defaults to MD5.
1036
     */
1037
    public Checksum getChecksum(AuthToken token, Identifier guid)
1038
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
1039
            InvalidRequest, NotImplemented 
1040
    {
1041
        logCrud.info("getChecksum");
1042
        return getChecksum(token, guid, "MD5");
1043
    }
1044

    
1045
    /**
1046
     * get the checksum for a document with the given algorithm
1047
     */
1048
    public Checksum getChecksum(AuthToken token, Identifier guid, 
1049
            String checksumAlgorithm) throws InvalidToken, ServiceFailure, 
1050
            NotAuthorized, NotFound, InvalidRequest, NotImplemented 
1051
    {
1052
        logCrud.info("getChecksum");
1053
        SystemMetadata sm = getSystemMetadata(token, guid);
1054
        Checksum cs = sm.getChecksum();
1055
        if(cs.getAlgorithm().toString().equals(checksumAlgorithm))
1056
        {
1057
            return cs;
1058
        }
1059
        else
1060
        {
1061
            if(checksumAlgorithm == null)
1062
            {
1063
                checksumAlgorithm = "MD5";
1064
            }
1065
            InputStream docStream = get(token, guid);
1066
            String checksum;
1067
            try
1068
            {
1069
                checksum = checksum(docStream, checksumAlgorithm);
1070
            }
1071
            catch(Exception e)
1072
            {
1073
                throw new ServiceFailure("1410", "Error getting checksum: " + e.getMessage());
1074
            }
1075
            Checksum c = new Checksum();
1076
            c.setAlgorithm(ChecksumAlgorithm.convert(checksumAlgorithm));
1077
            c.setValue(checksum);
1078
            return c;
1079
        }
1080
    }
1081

    
1082
    /**
1083
     * get log records.  
1084
     */
1085
    public Log getLogRecords(AuthToken token, Date fromDate, Date toDate, Event event)
1086
            throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest, 
1087
            NotImplemented 
1088
    {
1089
        /*System.out.println("=================== Getting log records ===================");
1090
        System.out.println("Current server time is: " + new Date());
1091
        if(fromDate != null)
1092
        {
1093
          System.out.println("query start time is " + fromDate);
1094
        }
1095
        if(toDate != null)
1096
        {
1097
          System.out.println("query end time is " + toDate);
1098
        }*/
1099
        Log log = new Log();
1100
        Vector<LogEntry> logs = new Vector<LogEntry>();
1101
        IdentifierManager im = IdentifierManager.getInstance();
1102
        EventLog el = EventLog.getInstance();
1103
        if(fromDate == null)
1104
        {
1105
            //System.out.println("setting fromdate from null");
1106
            fromDate = new Date(1);
1107
        }
1108
        if(toDate == null)
1109
        {
1110
            //System.out.println("setting todate from null");
1111
            toDate = new Date();
1112
        }
1113
        
1114
        //System.out.println("fromDate: " + fromDate);
1115
        //System.out.println("toDate: " + toDate);
1116
        
1117
        String report = el.getReport(null, null, null, null, 
1118
                new java.sql.Timestamp(fromDate.getTime()), 
1119
                new java.sql.Timestamp(toDate.getTime()), false);
1120
        
1121
        //System.out.println("report: " + report);
1122
        
1123
        String logEntry = "<logEntry>";
1124
        String endLogEntry = "</logEntry>";
1125
        int startIndex = 0;
1126
        int foundIndex = report.indexOf(logEntry, startIndex);
1127
        while(foundIndex != -1)
1128
        {
1129
            //parse out each entry
1130
            int endEntryIndex = report.indexOf(endLogEntry, foundIndex);
1131
            String entry = report.substring(foundIndex, endEntryIndex);
1132
            //System.out.println("entry: " + entry);
1133
            startIndex = endEntryIndex + endLogEntry.length();
1134
            foundIndex = report.indexOf(logEntry, startIndex);
1135
            
1136
            String entryId = getLogEntryField("entryid", entry);
1137
            String ipAddress = getLogEntryField("ipAddress", entry);
1138
            String principal = getLogEntryField("principal", entry);
1139
            String docid = getLogEntryField("docid", entry);
1140
            String eventS = getLogEntryField("event", entry);
1141
            String dateLogged = getLogEntryField("dateLogged", entry);
1142
            
1143
            LogEntry le = new LogEntry();
1144
            
1145
            Event e = Event.convert(eventS);
1146
            if(e == null)
1147
            { //skip any events that are not Dataone Crud events
1148
                continue;
1149
            }
1150
            le.setEvent(e);
1151
            Identifier entryid = new Identifier();
1152
            entryid.setValue(entryId);
1153
            le.setEntryId(entryid);
1154
            Identifier identifier = new Identifier();
1155
            try
1156
            {
1157
                //System.out.println("converting docid '" + docid + "' to a guid.");
1158
                if(docid == null || docid.trim().equals("") || docid.trim().equals("null"))
1159
                {
1160
                    continue;
1161
                }
1162
                docid = docid.substring(0, docid.lastIndexOf("."));
1163
                identifier.setValue(im.getGUID(docid, im.getLatestRevForLocalId(docid)));
1164
            }
1165
            catch(Exception ex)
1166
            { //try to get the guid, if that doesn't work, just use the local id
1167
                //throw new ServiceFailure("1030", "Error getting guid for localId " + 
1168
                //        docid + ": " + ex.getMessage());\
1169
                
1170
                //skip it if the guid can't be found
1171
                continue;
1172
            }
1173
            
1174
            le.setIdentifier(identifier);
1175
            le.setIpAddress(ipAddress);
1176
            Calendar c = Calendar.getInstance();
1177
            String year = dateLogged.substring(0, 4);
1178
            String month = dateLogged.substring(5, 7);
1179
            String date = dateLogged.substring(8, 10);
1180
            //System.out.println("year: " + year + " month: " + month + " day: " + date);
1181
            c.set(new Integer(year).intValue(), new Integer(month).intValue(), new Integer(date).intValue());
1182
            Date logDate = c.getTime();
1183
            le.setDateLogged(logDate);
1184
            NodeReference memberNode = new NodeReference();
1185
            memberNode.setValue(ipAddress);
1186
            le.setMemberNode(memberNode);
1187
            Principal princ = new Principal();
1188
            princ.setValue(principal);
1189
            le.setPrincipal(princ);
1190
            le.setUserAgent("metacat/RESTService");
1191
            
1192
            if(event == null)
1193
            {
1194
                logs.add(le);
1195
            }
1196
            
1197
            if(event != null &&
1198
               e.toString().toLowerCase().trim().equals(event.toString().toLowerCase().trim()))
1199
            {
1200
              logs.add(le);
1201
            }
1202
        }
1203
        
1204
        log.setLogEntryList(logs);
1205
        logCrud.info("getLogRecords");
1206
        return log;
1207
    }
1208
    
1209
    /**
1210
     * parse a logEntry and get the relavent field from it
1211
     * @param fieldname
1212
     * @param entry
1213
     * @return
1214
     */
1215
    private String getLogEntryField(String fieldname, String entry)
1216
    {
1217
        String begin = "<" + fieldname + ">";
1218
        String end = "</" + fieldname + ">";
1219
        //System.out.println("looking for " + begin + " and " + end + " in entry " + entry);
1220
        String s = entry.substring(entry.indexOf(begin) + begin.length(), entry.indexOf(end));
1221
        //System.out.println("entry " + fieldname + " : " + s);
1222
        return s;
1223
    }
1224

    
1225
    /**
1226
     * get the system metadata for a document with a specified guid.
1227
     */
1228
public SystemMetadata getSystemMetadata(AuthToken token, Identifier guid)
1229
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
1230
            InvalidRequest, NotImplemented {
1231
        
1232
        logMetacat.debug("CrudService.getSystemMetadata - for guid: " + guid.getValue());
1233
        
1234
        // Retrieve the session information from the AuthToken
1235
        // If the session is expired, then the user is 'public'
1236
        final SessionData sessionData = getSessionData(token);
1237
                
1238
        try {
1239
            IdentifierManager im = IdentifierManager.getInstance();
1240
            final String localId = im.getSystemMetadataLocalId(guid.getValue());
1241
            InputStream objectStream;
1242
            
1243
            try {
1244
                String username = "public";
1245
                String[] groupnames = null;
1246
                if(sessionData != null)
1247
                {
1248
                    username = sessionData.getUserName();
1249
                    groupnames = sessionData.getGroupNames();
1250
                }
1251
                
1252
                objectStream = readFromMetacat(localId, username, groupnames);
1253
                
1254
            } catch (PropertyNotFoundException e) {
1255
                e.printStackTrace();
1256
                throw new ServiceFailure("1090", "Property not found while reading system metadata from metacat: " + e.getMessage());
1257
            } catch (ClassNotFoundException e) {
1258
                e.printStackTrace();
1259
                throw new ServiceFailure("1090", "Class not found while reading system metadata from metacat: " + e.getMessage());
1260
            } catch (IOException e) {
1261
                e.printStackTrace();
1262
                throw new ServiceFailure("1090", "IOException while reading system metadata from metacat: " + e.getMessage());
1263
            } catch (SQLException e) {
1264
                e.printStackTrace();
1265
                throw new ServiceFailure("1090", "SQLException while reading system metadata from metacat: " + e.getMessage());
1266
            } catch (McdbException e) {
1267
                e.printStackTrace();
1268
                throw new ServiceFailure("1090", "Metacat DB Exception while reading system metadata from metacat: " + e.getMessage());
1269
            } catch (ParseLSIDException e) {
1270
                e.printStackTrace();
1271
                throw new NotFound("1060", "Error parsing LSID while reading system metadata from metacat: " + e.getMessage());
1272
            } catch (InsufficientKarmaException e) {
1273
                e.printStackTrace();
1274
                throw new NotAuthorized("1040", "User not authorized for get() on system metadata: " + e.getMessage());
1275
            }
1276
                        
1277
            // Deserialize the xml to create a SystemMetadata object
1278
            SystemMetadata sysmeta = deserializeSystemMetadata(objectStream);
1279
            String username = "public";
1280
            if(sessionData != null)
1281
            {
1282
                username = sessionData.getUserName();
1283
            }
1284
            EventLog.getInstance().log(metacatUrl,
1285
                    username, im.getLocalId(guid.getValue()), "read");
1286
            logCrud.info("getsystemmetadata D1GUID:" + guid.getValue()  + 
1287
                    ":D1SYSMETADATA:"+ localId + ":");
1288
            return sysmeta;
1289
            
1290
        } catch (McdbDocNotFoundException e) {
1291
            //e.printStackTrace();
1292
            throw new NotFound("1040", e.getMessage());
1293
        }                
1294
    }
1295
    
1296
    /**
1297
     * parse the date in the systemMetadata
1298
     * @param s
1299
     * @return
1300
     * @throws Exception
1301
     */
1302
    public Date parseDate(String s)
1303
      throws Exception
1304
    {
1305
        /* TODO:
1306
         * This method should be replaced by a DateFormatter
1307
         */
1308
        Date d = null;
1309
        int tIndex = s.indexOf("T");
1310
        int zIndex = s.indexOf("Z");
1311
        if(tIndex != -1 && zIndex != -1)
1312
        { //parse a date that looks like 2010-05-18T21:12:54.362Z
1313
            //System.out.println("original date: " + s);
1314
            
1315
            String date = s.substring(0, tIndex);
1316
            String year = date.substring(0, date.indexOf("-"));
1317
            String month = date.substring(date.indexOf("-") + 1, date.lastIndexOf("-"));
1318
            String day = date.substring(date.lastIndexOf("-") + 1, date.length());
1319
            /*System.out.println("date: " + "year: " + new Integer(year).intValue() + 
1320
                    " month: " + new Integer(month).intValue() + " day: " + 
1321
                    new Integer(day).intValue());
1322
            */
1323
            String time = s.substring(tIndex + 1, zIndex);
1324
            String hour = time.substring(0, time.indexOf(":"));
1325
            String minute = time.substring(time.indexOf(":") + 1, time.lastIndexOf(":"));
1326
            String seconds = "00";
1327
            String milliseconds = "00";
1328
            if(time.indexOf(".") != -1)
1329
            {
1330
                seconds = time.substring(time.lastIndexOf(":") + 1, time.indexOf("."));
1331
                milliseconds = time.substring(time.indexOf(".") + 1, time.length());
1332
            }
1333
            else
1334
            {
1335
                seconds = time.substring(time.lastIndexOf(":") + 1, time.length());
1336
            }
1337
            /*System.out.println("time: " + "hour: " + new Integer(hour).intValue() + 
1338
                    " minute: " + new Integer(minute).intValue() + " seconds: " + 
1339
                    new Integer(seconds).intValue() + " milli: " + 
1340
                    new Integer(milliseconds).intValue());*/
1341
            
1342
            //d = DateFormat.getDateTimeInstance().parse(date + " " + time);
1343
            Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT-0")/*TimeZone.getDefault()*/);
1344
            c.set(new Integer(year).intValue(), new Integer(month).intValue() - 1, 
1345
                  new Integer(day).intValue(), new Integer(hour).intValue(), 
1346
                  new Integer(minute).intValue(), new Integer(seconds).intValue());
1347
            c.set(Calendar.MILLISECOND, new Integer(milliseconds).intValue());
1348
            d = new Date(c.getTimeInMillis());
1349
            //System.out.println("d: " + d);
1350
            return d;
1351
        }
1352
        else
1353
        {  //if it's not in the expected format, try the formatter
1354
            return DateFormat.getDateTimeInstance().parse(s);
1355
        }
1356
    }
1357

    
1358
    /*
1359
     * Look up the information on the session using the token provided in
1360
     * the AuthToken.  The Session should have all relevant user information.
1361
     * If the session has expired or is invalid, the 'public' session will
1362
     * be returned, giving the user anonymous access.
1363
     */
1364
    public static SessionData getSessionData(AuthToken token) {
1365
        SessionData sessionData = null;
1366
        String sessionId = "PUBLIC";
1367
        if (token != null) {
1368
            sessionId = token.getToken();
1369
        }
1370
        
1371
        // if the session id is registered in SessionService, get the
1372
        // SessionData for it. Otherwise, use the public session.
1373
        //System.out.println("sessionid: " + sessionId);
1374
        if (sessionId != null &&
1375
            !sessionId.toLowerCase().equals("public") &&
1376
            SessionService.getInstance().isSessionRegistered(sessionId)) 
1377
        {
1378
            sessionData = SessionService.getInstance().getRegisteredSession(sessionId);
1379
        } else {
1380
            sessionData = SessionService.getInstance().getPublicSession();
1381
        }
1382
        
1383
        return sessionData;
1384
    }
1385

    
1386
    /** 
1387
     * Determine if a given object should be treated as an XML science metadata
1388
     * object. 
1389
     * 
1390
     * TODO: This test should be externalized in a configuration dictionary rather than being hardcoded.
1391
     * 
1392
     * @param sysmeta the SystemMetadata describig the object
1393
     * @return true if the object should be treated as science metadata
1394
     */
1395
    private boolean isScienceMetadata(SystemMetadata sysmeta) {
1396
        /*boolean scimeta = false;
1397
        //TODO: this should be read from a .properties file instead of being hard coded
1398
        switch (sysmeta.getObjectFormat()) {
1399
            case EML_2_1_0: scimeta = true; break;
1400
            case EML_2_0_1: scimeta = true; break;
1401
            case EML_2_0_0: scimeta = true; break;
1402
            case FGDC_STD_001_1_1999: scimeta = true; break;
1403
            case FGDC_STD_001_1998: scimeta = true; break;
1404
            case NCML_2_2: scimeta = true; break;
1405
            case DSPACE_METS_SIP_1_0: scimeta = true; break;
1406
        }
1407
        
1408
        return scimeta;*/
1409
        
1410
        return MetadataTypeRegister.isMetadataType(sysmeta.getObjectFormat());
1411
    }
1412

    
1413
    /**
1414
     * insert a data doc
1415
     * @param object
1416
     * @param guid
1417
     * @param sessionData
1418
     * @throws ServiceFailure
1419
     * @returns localId of the data object inserted
1420
     */
1421
    private String insertDataObject(InputStream object, Identifier guid, 
1422
            SessionData sessionData) throws ServiceFailure {
1423
        
1424
        String username = "public";
1425
        String[] groups = null;
1426
        if(sessionData != null)
1427
        {
1428
          username = sessionData.getUserName();
1429
          groups = sessionData.getGroupNames();
1430
        }
1431

    
1432
        // generate guid/localId pair for object
1433
        logMetacat.debug("Generating a guid/localId mapping");
1434
        IdentifierManager im = IdentifierManager.getInstance();
1435
        String localId = im.generateLocalId(guid.getValue(), 1);
1436

    
1437
        try {
1438
            logMetacat.debug("Case DATA: starting to write to disk.");
1439
            if (DocumentImpl.getDataFileLockGrant(localId)) {
1440
    
1441
                // Save the data file to disk using "localId" as the name
1442
                try {
1443
                    String datafilepath = PropertyService.getProperty("application.datafilepath");
1444
    
1445
                    File dataDirectory = new File(datafilepath);
1446
                    dataDirectory.mkdirs();
1447
    
1448
                    File newFile = writeStreamToFile(dataDirectory, localId, object);
1449
    
1450
                    // TODO: Check that the file size matches SystemMetadata
1451
                    //                        long size = newFile.length();
1452
                    //                        if (size == 0) {
1453
                    //                            throw new IOException("Uploaded file is 0 bytes!");
1454
                    //                        }
1455
    
1456
                    // Register the file in the database (which generates an exception
1457
                    // if the localId is not acceptable or other untoward things happen
1458
                    try {
1459
                        logMetacat.debug("Registering document...");
1460
                        DocumentImpl.registerDocument(localId, "BIN", localId,
1461
                                username, groups);
1462
                        logMetacat.debug("Registration step completed.");
1463
                    } catch (SQLException e) {
1464
                        //newFile.delete();
1465
                        logMetacat.debug("SQLE: " + e.getMessage());
1466
                        e.printStackTrace(System.out);
1467
                        throw new ServiceFailure("1190", "Registration failed: " + e.getMessage());
1468
                    } catch (AccessionNumberException e) {
1469
                        //newFile.delete();
1470
                        logMetacat.debug("ANE: " + e.getMessage());
1471
                        e.printStackTrace(System.out);
1472
                        throw new ServiceFailure("1190", "Registration failed: " + e.getMessage());
1473
                    } catch (Exception e) {
1474
                        //newFile.delete();
1475
                        logMetacat.debug("Exception: " + e.getMessage());
1476
                        e.printStackTrace(System.out);
1477
                        throw new ServiceFailure("1190", "Registration failed: " + e.getMessage());
1478
                    }
1479
    
1480
                    logMetacat.debug("Logging the creation event.");
1481
                    EventLog.getInstance().log(metacatUrl,
1482
                            username, localId, "create");
1483
    
1484
                    // Schedule replication for this data file
1485
                    logMetacat.debug("Scheduling replication.");
1486
                    ForceReplicationHandler frh = new ForceReplicationHandler(
1487
                            localId, "create", false, null);
1488
    
1489
                } catch (PropertyNotFoundException e) {
1490
                    throw new ServiceFailure("1190", "Could not lock file for writing:" + e.getMessage());
1491
                }
1492
            }
1493
            return localId;
1494
        } catch (Exception e) {
1495
            // Could not get a lock on the document, so we can not update the file now
1496
            throw new ServiceFailure("1190", "Failed to lock file: " + e.getMessage());
1497
        }
1498
    }
1499

    
1500
    /**
1501
     * write a file to a stream
1502
     * @param dir
1503
     * @param fileName
1504
     * @param data
1505
     * @return
1506
     * @throws ServiceFailure
1507
     */
1508
    private File writeStreamToFile(File dir, String fileName, InputStream data) 
1509
        throws ServiceFailure {
1510
        
1511
        File newFile = new File(dir, fileName);
1512
        logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
1513

    
1514
        try {
1515
            if (newFile.createNewFile()) {
1516
                // write data stream to desired file
1517
                OutputStream os = new FileOutputStream(newFile);
1518
                long length = IOUtils.copyLarge(data, os);
1519
                os.flush();
1520
                os.close();
1521
            } else {
1522
                logMetacat.debug("File creation failed, or file already exists.");
1523
                throw new ServiceFailure("1190", "File already exists: " + fileName);
1524
            }
1525
        } catch (FileNotFoundException e) {
1526
            logMetacat.debug("FNF: " + e.getMessage());
1527
            throw new ServiceFailure("1190", "File not found: " + fileName + " " 
1528
                    + e.getMessage());
1529
        } catch (IOException e) {
1530
            logMetacat.debug("IOE: " + e.getMessage());
1531
            throw new ServiceFailure("1190", "File was not written: " + fileName 
1532
                    + " " + e.getMessage());
1533
        }
1534

    
1535
        return newFile;
1536
    }
1537

    
1538
    /**
1539
     * insert a systemMetadata doc, return the localId of the sysmeta
1540
     */
1541
    private String insertSystemMetadata(SystemMetadata sysmeta, SessionData sessionData) 
1542
        throws ServiceFailure 
1543
    {
1544
        logMetacat.debug("Starting to insert SystemMetadata...");
1545
    
1546
        // generate guid/localId pair for sysmeta
1547
        Identifier sysMetaGuid = new Identifier();
1548
        sysMetaGuid.setValue(DocumentUtil.generateDocumentId(1));
1549
        sysmeta.setDateSysMetadataModified(new Date());
1550
        System.out.println("****inserting new system metadata with modified date " + 
1551
                sysmeta.getDateSysMetadataModified());
1552

    
1553
        String xml = new String(serializeSystemMetadata(sysmeta).toByteArray());
1554
        System.out.println("sysmeta: " + xml);
1555
        String localId = insertDocument(xml, sysMetaGuid, sessionData, true);
1556
        System.out.println("sysmeta inserted with localId " + localId);
1557
        //insert the system metadata doc id into the systemmetadata table to 
1558
        //link it to the data or metadata document
1559
        IdentifierManager.getInstance().createSystemMetadataMapping(
1560
                sysmeta.getIdentifier().getValue(), sysMetaGuid.getValue());
1561
        return localId;
1562
    }
1563
    
1564
    /**
1565
     * update a systemMetadata doc
1566
     */
1567
    private void updateSystemMetadata(SystemMetadata sm, SessionData sessionData)
1568
      throws ServiceFailure
1569
    {
1570
        try
1571
        {
1572
            String smId = IdentifierManager.getInstance().getSystemMetadataLocalId(sm.getIdentifier().getValue());
1573
            System.out.println("setting date modified to " + new Date());
1574
            sm.setDateSysMetadataModified(new Date());
1575
            String xml = new String(serializeSystemMetadata(sm).toByteArray());
1576
            String localId = updateDocument(xml, sm.getIdentifier(), null, sessionData, true);
1577
            IdentifierManager.getInstance().updateSystemMetadataMapping(sm.getIdentifier().getValue(), localId);
1578
        }
1579
        catch(Exception e)
1580
        {
1581
            throw new ServiceFailure("1030", "Error updating system metadata: " + e.getMessage());
1582
        }
1583
    }
1584
    
1585
    private String insertDocument(String xml, Identifier guid, SessionData sessionData)
1586
        throws ServiceFailure
1587
    {
1588
        return insertDocument(xml, guid, sessionData, false);
1589
    }
1590
    
1591
    /**
1592
     * insert a document
1593
     * NOTE: this method shouldn't be used from the update or create() methods.  
1594
     * we shouldn't be putting the science metadata or data objects into memory.
1595
     */
1596
    private String insertDocument(String xml, Identifier guid, SessionData sessionData,
1597
            boolean isSystemMetadata)
1598
        throws ServiceFailure
1599
    {
1600
        return insertOrUpdateDocument(xml, guid, sessionData, "insert", isSystemMetadata);
1601
    }
1602
    
1603
    /**
1604
     * insert a document from a stream
1605
     */
1606
    private String insertDocument(InputStream is, Identifier guid, SessionData sessionData)
1607
      throws IOException, ServiceFailure
1608
    {
1609
        //HACK: change this eventually.  we should not be converting the stream to a string
1610
        String xml = IOUtils.toString(is);
1611
        return insertDocument(xml, guid, sessionData);
1612
    }
1613
    
1614
    /**
1615
     * update a document
1616
     * NOTE: this method shouldn't be used from the update or create() methods.  
1617
     * we shouldn't be putting the science metadata or data objects into memory.
1618
     */
1619
    private String updateDocument(String xml, Identifier obsoleteGuid, 
1620
            Identifier guid, SessionData sessionData, boolean isSystemMetadata)
1621
        throws ServiceFailure
1622
    {
1623
        return insertOrUpdateDocument(xml, obsoleteGuid, sessionData, "update", isSystemMetadata);
1624
    }
1625
    
1626
    /**
1627
     * update a document from a stream
1628
     */
1629
    private String updateDocument(InputStream is, Identifier obsoleteGuid, 
1630
            Identifier guid, SessionData sessionData, boolean isSystemMetadata)
1631
      throws IOException, ServiceFailure
1632
    {
1633
        //HACK: change this eventually.  we should not be converting the stream to a string
1634
        String xml = IOUtils.toString(is);
1635
        String localId = updateDocument(xml, obsoleteGuid, guid, sessionData, isSystemMetadata);
1636
        IdentifierManager im = IdentifierManager.getInstance();
1637
        if(guid != null)
1638
        {
1639
          im.createMapping(guid.getValue(), localId);
1640
        }
1641
        return localId;
1642
    }
1643
    
1644
    /**
1645
     * insert a document, return the id of the document that was inserted
1646
     */
1647
    protected String insertOrUpdateDocument(String xml, Identifier guid, 
1648
            SessionData sessionData, String insertOrUpdate, boolean isSystemMetadata) 
1649
        throws ServiceFailure {
1650
        logMetacat.debug("Starting to insert xml document...");
1651
        IdentifierManager im = IdentifierManager.getInstance();
1652

    
1653
        // generate guid/localId pair for sysmeta
1654
        String localId = null;
1655
        if(insertOrUpdate.equals("insert"))
1656
        {
1657
            localId = im.generateLocalId(guid.getValue(), 1, isSystemMetadata);
1658
        }
1659
        else
1660
        {
1661
            //localid should already exist in the identifier table, so just find it
1662
            try
1663
            {
1664
                System.out.println("updating guid " + guid.getValue());
1665
                if(!isSystemMetadata)
1666
                {
1667
                    System.out.println("looking in identifier table for guid " + guid.getValue());
1668
                    localId = im.getLocalId(guid.getValue());
1669
                }
1670
                else
1671
                {
1672
                    System.out.println("looking in systemmetadata table for guid " + guid.getValue());
1673
                    localId = im.getSystemMetadataLocalId(guid.getValue());
1674
                }
1675
                System.out.println("localId: " + localId);
1676
                //increment the revision
1677
                String docid = localId.substring(0, localId.lastIndexOf("."));
1678
                String revS = localId.substring(localId.lastIndexOf(".") + 1, localId.length());
1679
                int rev = new Integer(revS).intValue();
1680
                rev++;
1681
                docid = docid + "." + rev;
1682
                localId = docid;
1683
                System.out.println("incremented localId: " + localId);
1684
            }
1685
            catch(McdbDocNotFoundException e)
1686
            {
1687
                throw new ServiceFailure("1030", "CrudService.insertOrUpdateDocument(): " +
1688
                    "guid " + guid.getValue() + " should have been in the identifier table, but it wasn't: " + e.getMessage());
1689
            }
1690
        }
1691
        logMetacat.debug("Metadata guid|localId: " + guid.getValue() + "|" +
1692
                localId);
1693

    
1694
        String[] action = new String[1];
1695
        action[0] = insertOrUpdate;
1696
        params.put("action", action);
1697
        String[] docid = new String[1];
1698
        docid[0] = localId;
1699
        params.put("docid", docid);
1700
        String[] doctext = new String[1];
1701
        doctext[0] = xml;
1702
        logMetacat.debug(doctext[0]);
1703
        params.put("doctext", doctext);
1704
        
1705
        // TODO: refactor handleInsertOrUpdateAction() to not output XML directly
1706
        // onto output stream, or alternatively, capture that and parse it to 
1707
        // generate the right exceptions
1708
        //ByteArrayOutputStream output = new ByteArrayOutputStream();
1709
        //PrintWriter pw = new PrintWriter(output);
1710
        String username = "public";
1711
        String[] groupnames = null;
1712
        if(sessionData != null)
1713
        {
1714
            username = sessionData.getUserName();
1715
            groupnames = sessionData.getGroupNames();
1716
        }
1717
        String result = handler.handleInsertOrUpdateAction(metacatUrl, null, 
1718
                            null, params, username, groupnames);
1719
        if(result.indexOf("<error>") != -1)
1720
        {
1721
            throw new ServiceFailure("1000", "Error inserting or updating document: " + result);
1722
        }
1723
        //String outputS = new String(output.toByteArray());
1724
        logMetacat.debug("CrudService.insertDocument - Metacat returned: " + result);
1725
        logMetacat.debug("Finsished inserting xml document with id " + localId);
1726
        return localId;
1727
    }
1728
    
1729
    /**
1730
     * serialize a dataone type
1731
     */
1732
    private void serializeServiceType(Class type, Object object, OutputStream out)
1733
        throws JiBXException
1734
    {
1735
        IBindingFactory bfact = BindingDirectory.getFactory(type);
1736
        IMarshallingContext mctx = bfact.createMarshallingContext();
1737
        mctx.marshalDocument(object, "UTF-8", null, out);
1738
    }
1739
    
1740
    /**
1741
     * serialize a system metadata doc
1742
     * @param sysmeta
1743
     * @return
1744
     * @throws ServiceFailure
1745
     */
1746
    public static ByteArrayOutputStream serializeSystemMetadata(SystemMetadata sysmeta) 
1747
        throws ServiceFailure {
1748
        IBindingFactory bfact;
1749
        ByteArrayOutputStream sysmetaOut = null;
1750
        try {
1751
            bfact = BindingDirectory.getFactory(SystemMetadata.class);
1752
            IMarshallingContext mctx = bfact.createMarshallingContext();
1753
            sysmetaOut = new ByteArrayOutputStream();
1754
            mctx.marshalDocument(sysmeta, "UTF-8", null, sysmetaOut);
1755
        } catch (JiBXException e) {
1756
            e.printStackTrace();
1757
            throw new ServiceFailure("1190", "Failed to serialize and insert SystemMetadata: " + e.getMessage());
1758
        }
1759
        
1760
        return sysmetaOut;
1761
    }
1762
    
1763
    /**
1764
     * deserialize a system metadata doc
1765
     * @param xml
1766
     * @return
1767
     * @throws ServiceFailure
1768
     */
1769
    public static SystemMetadata deserializeSystemMetadata(InputStream xml) 
1770
        throws ServiceFailure {
1771
        try {
1772
            IBindingFactory bfact = BindingDirectory.getFactory(SystemMetadata.class);
1773
            IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
1774
            SystemMetadata sysmeta = (SystemMetadata) uctx.unmarshalDocument(xml, null);
1775
            return sysmeta;
1776
        } catch (JiBXException e) {
1777
            e.printStackTrace();
1778
            throw new ServiceFailure("1190", "Failed to deserialize and insert SystemMetadata: " + e.getMessage());
1779
        }    
1780
    }
1781
    
1782
    /**
1783
     * read a document from metacat and return the InputStream
1784
     * 
1785
     * @param localId
1786
     * @param username
1787
     * @param groups
1788
     * @return
1789
     * @throws InsufficientKarmaException
1790
     * @throws ParseLSIDException
1791
     * @throws PropertyNotFoundException
1792
     * @throws McdbException
1793
     * @throws SQLException
1794
     * @throws ClassNotFoundException
1795
     * @throws IOException
1796
     */
1797
    private InputStream readFromMetacat(String localId, String username, String[] groups)
1798
        throws InsufficientKarmaException, ParseLSIDException,
1799
        PropertyNotFoundException, McdbException, SQLException, 
1800
        ClassNotFoundException, IOException
1801
    {
1802
        File tmpDir;
1803
        try
1804
        {
1805
            tmpDir = new File(PropertyService.getProperty("application.tempDir"));
1806
        }
1807
        catch(PropertyNotFoundException pnfe)
1808
        {
1809
            logMetacat.error("ResourceHandler.writeMMPPartstoFiles: " +
1810
                    "application.tmpDir not found.  Using /tmp instead.");
1811
            tmpDir = new File("/tmp");
1812
        }
1813
        Date d = new Date();
1814
        final File outputFile = new File(tmpDir, "metacat.output." + d.getTime());
1815
        FileOutputStream dataSink = new FileOutputStream(outputFile);
1816
        
1817
        handler.readFromMetacat(metacatUrl, null, 
1818
                dataSink, localId, "xml",
1819
                username, 
1820
                groups, true, params);
1821
        
1822
        //set a timer to clean up the temp files
1823
        Timer t = new Timer();
1824
        TimerTask tt = new TimerTask() {
1825
            @Override
1826
            public void run()
1827
            {
1828
                outputFile.delete();
1829
            }
1830
        };
1831
        t.schedule(tt, 20000); //schedule after 20 secs
1832
        
1833
        InputStream objectStream = new FileInputStream(outputFile);
1834
        return objectStream;
1835
    }
1836
    
1837
    /**
1838
     * return an MD5 checksum for the stream
1839
     * @param is
1840
     * @return
1841
     */
1842
    public static String checksum(InputStream is)
1843
        throws Exception
1844
    {
1845
        return checksum(is, "MD5");
1846
    }
1847
    
1848
    /**
1849
     * produce a checksum for item using the given algorithm
1850
     */
1851
    public static String checksum(InputStream is, String algorithm)
1852
      throws Exception
1853
    {        
1854
        return ServiceTypeUtil.checksum(is, ChecksumAlgorithm.convert(algorithm)).getValue();
1855
    }
1856
    
1857
    /**
1858
     * parse the metacat date which looks like 2010-06-08 (YYYY-MM-DD) into
1859
     * a proper date object
1860
     * @param date
1861
     * @return
1862
     */
1863
    private Date parseMetacatDate(String date)
1864
    {
1865
        String year = date.substring(0, 4);
1866
        String month = date.substring(5, 7);
1867
        String day = date.substring(8, 10);
1868
        Calendar c = Calendar.getInstance(TimeZone.getDefault());
1869
        c.set(new Integer(year).intValue(), 
1870
              new Integer(month).intValue(), 
1871
              new Integer(day).intValue());
1872
        System.out.println("time in parseMetacatDate: " + c.getTime());
1873
        return c.getTime();
1874
    }
1875
    
1876
    /**
1877
     * find the size (in bytes) of a stream
1878
     * @param is
1879
     * @return
1880
     * @throws IOException
1881
     */
1882
    private long sizeOfStream(InputStream is)
1883
        throws IOException
1884
    {
1885
        long size = 0;
1886
        byte[] b = new byte[1024];
1887
        int numread = is.read(b, 0, 1024);
1888
        while(numread != -1)
1889
        {
1890
            size += numread;
1891
            numread = is.read(b, 0, 1024);
1892
        }
1893
        return size;
1894
    }
1895
    
1896
    /**
1897
     * create system metadata with a specified id, doc and format
1898
     */
1899
    private SystemMetadata createSystemMetadata(String localId, AuthToken token)
1900
      throws Exception
1901
    {
1902
        IdentifierManager im = IdentifierManager.getInstance();
1903
        Hashtable<String, Object> docInfo = im.getDocumentInfo(localId);
1904
        
1905
        //get the document text
1906
        int rev = im.getLatestRevForLocalId(localId);
1907
        Identifier identifier = new Identifier();
1908
        try
1909
        {
1910
            identifier.setValue(im.getGUID(localId, rev));
1911
        }
1912
        catch(McdbDocNotFoundException mcdbe)
1913
        { //we're creating a new SM doc for a doc that is not in the identifier table
1914
          //so we need to add it to
1915
            System.out.println("No guid in the identifier table.  adding it for " + localId);
1916
            im.createMapping(localId, localId);
1917
            System.out.println("mapping created for " + localId);
1918
            AccessionNumber accNum = new AccessionNumber(localId, "NONE");
1919
            identifier.setValue(im.getGUID(accNum.getDocid(), rev));
1920
        }
1921
        
1922
        System.out.println("creating system metadata for guid " + identifier.getValue());
1923
        InputStream is = this.get(token, identifier);
1924
        
1925
        SystemMetadata sm = new SystemMetadata();
1926
        //set the id
1927
        sm.setIdentifier(identifier);
1928
        
1929
        //set the object format
1930
        String doctype = (String)docInfo.get("doctype");
1931
        ObjectFormat format = ObjectFormat.convert((String)docInfo.get("doctype"));
1932
        if(format == null)
1933
        {
1934
            if(doctype.trim().equals("BIN"))
1935
            {
1936
                format = ObjectFormat.OCTET_STREAM;
1937
            }
1938
            else
1939
            {
1940
                format = ObjectFormat.convert("text/plain");
1941
            }
1942
        }
1943
        sm.setObjectFormat(format);
1944
        
1945
        //create the checksum
1946
        String checksumS = checksum(is);
1947
        ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
1948
        Checksum checksum = new Checksum();
1949
        checksum.setValue(checksumS);
1950
        checksum.setAlgorithm(ca);
1951
        sm.setChecksum(checksum);
1952
        
1953
        //set the size
1954
        is = this.get(token, identifier);
1955
        sm.setSize(sizeOfStream(is));
1956
        
1957
        //submitter
1958
        Principal p = new Principal();
1959
        p.setValue((String)docInfo.get("user_owner"));
1960
        sm.setSubmitter(p);
1961
        sm.setRightsHolder(p);
1962
        try
1963
        {
1964
            Date dateCreated = parseMetacatDate((String)docInfo.get("date_created"));
1965
            sm.setDateUploaded(dateCreated);
1966
            Date dateUpdated = parseMetacatDate((String)docInfo.get("date_updated"));
1967
            sm.setDateSysMetadataModified(dateUpdated);
1968
        }
1969
        catch(Exception e)
1970
        {
1971
            System.out.println("POSSIBLE ERROR: couldn't parse a date: " + e.getMessage());
1972
            Date dateCreated = new Date();
1973
            sm.setDateUploaded(dateCreated);
1974
            Date dateUpdated = new Date();
1975
            sm.setDateSysMetadataModified(dateUpdated);
1976
        }
1977
        NodeReference nr = new NodeReference();
1978
        //TODO: this should be set to be something more meaningful once the registry is up
1979
        nr.setValue("metacat");
1980
        sm.setOriginMemberNode(nr);
1981
        sm.setAuthoritativeMemberNode(nr);
1982
        return sm;
1983
    }
1984
    
1985
    /**
1986
     * create the listObjects pathQuery document
1987
     */
1988
    private String createListObjectsPathQueryDocument()
1989
        throws PropertyNotFoundException
1990
    {
1991
        String s = "<pathquery>";
1992
        s += "<returndoctype>" + PropertyService.getProperty("crudService.listObjects.ReturnDoctype") + "</returndoctype>";
1993
        s += "<returnfield>" + PropertyService.getProperty("crudService.listObjects.ReturnField.1") + "</returnfield>";
1994
        s += "<returnfield>" + PropertyService.getProperty("crudService.listObjects.ReturnField.2") + "</returnfield>";
1995
        s += "<returnfield>" + PropertyService.getProperty("crudService.listObjects.ReturnField.3") + "</returnfield>";
1996
        s += "<returnfield>" + PropertyService.getProperty("crudService.listObjects.ReturnField.4") + "</returnfield>";
1997
        s += "<returnfield>" + PropertyService.getProperty("crudService.listObjects.ReturnField.5") + "</returnfield>";
1998
        s += "<returnfield>" + PropertyService.getProperty("crudService.listObjects.ReturnField.6") + "</returnfield>";
1999
        s += "<returnfield>" + PropertyService.getProperty("crudService.listObjects.ReturnField.7") + "</returnfield>";
2000
        s += "<querygroup operator=\"UNION\"><queryterm casesensitive=\"false\" searchmode=\"contains\">";
2001
        s += "<value>%</value>"; 
2002
        s += "<pathexpr>" + PropertyService.getProperty("crudService.listObjects.ReturnField.3") + "</pathexpr>";
2003
        s += "</queryterm></querygroup></pathquery>";
2004
  
2005
        return s;
2006
    }
2007
}
2008

    
(1-1/4)