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: leinfelder $'
7
 *     '$Date: 2011-06-07 10:27:25 -0700 (Tue, 07 Jun 2011) $'
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.util.Date;
26
import java.util.Hashtable;
27
import java.util.Timer;
28

    
29
import org.apache.log4j.Logger;
30
import org.dataone.service.cn.CNCore;
31
import org.dataone.service.exceptions.IdentifierNotUnique;
32
import org.dataone.service.exceptions.InsufficientResources;
33
import org.dataone.service.exceptions.InvalidRequest;
34
import org.dataone.service.exceptions.InvalidSystemMetadata;
35
import org.dataone.service.exceptions.InvalidToken;
36
import org.dataone.service.exceptions.NotAuthorized;
37
import org.dataone.service.exceptions.NotFound;
38
import org.dataone.service.exceptions.NotImplemented;
39
import org.dataone.service.exceptions.ServiceFailure;
40
import org.dataone.service.exceptions.UnsupportedType;
41
import org.dataone.service.types.Event;
42
import org.dataone.service.types.Identifier;
43
import org.dataone.service.types.Log;
44
import org.dataone.service.types.NodeList;
45
import org.dataone.service.types.ObjectFormat;
46
import org.dataone.service.types.ObjectFormatIdentifier;
47
import org.dataone.service.types.ObjectFormatList;
48
import org.dataone.service.types.Session;
49
import org.dataone.service.types.SystemMetadata;
50
import org.joda.time.DateTime;
51

    
52
import edu.ucsb.nceas.metacat.EventLog;
53
import edu.ucsb.nceas.metacat.IdentifierManager;
54
import edu.ucsb.nceas.metacat.replication.ForceReplicationSystemMetadataHandler;
55

    
56
/**
57
 * 
58
 * Implements DataONE MemberNode CRUD API for Metacat. 
59
 * 
60
 * @author Matthew Jones
61
 */
62
public class CNCoreImpl implements CNCore
63
{
64
    private static CNCoreImpl instance = null;
65

    
66
    private Logger logMetacat = null;
67
        
68
    /**
69
     * singleton accessor
70
     */
71
    public static CNCoreImpl getInstance() 
72
    {
73
      if (instance == null) {
74
        instance = new CNCoreImpl();
75
      }
76
      
77
      return instance;
78
    }
79
    
80
    /**
81
     * Constructor, private for singleton access
82
     */
83
    private CNCoreImpl() {
84
        logMetacat = Logger.getLogger(CNCoreImpl.class);
85
        
86
    }
87
    
88
    /**
89
     * register System Metadata
90
     */
91
    public boolean registerSystemMetaData(Session session, Identifier guid,
92
            SystemMetadata sysmeta) throws NotImplemented, NotAuthorized,
93
            ServiceFailure, InvalidRequest {
94
        logMetacat.debug("Starting CrudService.create()...");
95
        
96
        // TODO: control who can call this?
97
        if (session == null) {
98
            //TODO: many of the thrown exceptions do not use the correct error codes
99
            //check these against the docs and correct them
100
            throw new NotAuthorized("4861", "No Session - could not authorize to registration." +
101
                    "  If you are not logged in, please do so and retry the request.");
102
        }
103
        
104
        // verify that guid == SystemMetadata.getIdentifier()
105
        logMetacat.debug("Comparing guid|sysmeta_guid: " + guid.getValue() + "|" + sysmeta.getIdentifier().getValue());
106
        if (!guid.getValue().equals(sysmeta.getIdentifier().getValue())) {
107
            throw new InvalidRequest("4863", 
108
                "GUID in method call (" + guid.getValue() + ") does not match GUID in system metadata (" +
109
                sysmeta.getIdentifier().getValue() + ").");
110
        }
111

    
112
        logMetacat.debug("Checking if identifier exists...");
113
        // Check that the identifier does not already exist
114
        IdentifierManager im = IdentifierManager.getInstance();
115
        if (im.identifierExists(guid.getValue())) {
116
            throw new InvalidRequest("4863", 
117
                "GUID is already in use by an existing object.");
118
        }
119

    
120
        // insert the system metadata into the object store
121
        logMetacat.debug("Starting to insert SystemMetadata...");
122
        sysmeta.setDateSysMetadataModified(new Date());
123
        try {
124
			IdentifierManager.getInstance().createSystemMetadata(sysmeta);
125
			IdentifierManager.getInstance().updateSystemMetadata(sysmeta);
126
			// force replication of this record
127
			ForceReplicationSystemMetadataHandler forceReplication = new ForceReplicationSystemMetadataHandler(guid.getValue(), null);
128
		} catch (Exception e) {
129
            throw new ServiceFailure("4862", "Error inserting system metadata: " + e.getClass() + ": " + e.getMessage());
130
		}
131
        
132
        logMetacat.debug("Returning from registerSystemMetadata");
133
        EventLog.getInstance().log(null, session.getSubject().getValue(), guid.getValue(), "registerSystemMetadata");
134
        return true;
135
    }
136

    
137

    
138
  @Override
139
  public Log getLogRecords(Session arg0, DateTime arg1, DateTime arg2,
140
    Event arg3) throws InvalidToken, InvalidRequest, ServiceFailure,
141
    NotAuthorized, NotImplemented {
142
  	  // TODO Auto-generated method stub
143
  	  return null;
144
    }
145
   @Override
146
	 public ObjectFormat getFormat(ObjectFormatIdentifier arg0) 
147
	   throws InvalidRequest, ServiceFailure, NotFound, InsufficientResources, 
148
	   NotImplemented {
149
		 // TODO Auto-generated method stub
150
		 return null;
151
	}
152

    
153
	@Override
154
	public NodeList listNodes() throws NotImplemented, ServiceFailure {
155
		// TODO Auto-generated method stub
156
		return null;
157
	}
158

    
159
	@Override
160
  public ObjectFormatList listFormats() throws InvalidRequest, ServiceFailure,
161
    NotFound, InsufficientResources, NotImplemented {
162
	  // TODO Auto-generated method stub
163
	  return null;
164
  }
165

    
166
}
167

    
(1-1/5)