Project

General

Profile

1
/**
2
 * Copyright 2006 OCLC Online Computer Library Center Licensed under the Apache
3
 * License, Version 2.0 (the "License"); you may not use this file except in
4
 * compliance with the License. You may obtain a copy of the License at
5
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or
6
 * agreed to in writing, software distributed under the License is distributed on
7
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
8
 * or implied. See the License for the specific language governing permissions and
9
 * limitations under the License.
10
 */
11
package edu.ucsb.nceas.metacat.oaipmh.provider.server.catalog;
12

    
13
import java.util.HashMap;
14
import java.util.Iterator;
15
import java.util.Properties;
16
import java.util.StringTokenizer;
17

    
18
import edu.ucsb.nceas.metacat.oaipmh.provider.server.OAIHandler;
19
import edu.ucsb.nceas.metacat.service.PropertyService;
20
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
21

    
22
import org.apache.log4j.Logger;
23

    
24
import ORG.oclc.oai.server.catalog.RecordFactory;
25

    
26

    
27
/**
28
 * MetacatRecordFactory converts native XML "items" to "record" Strings. This
29
 * factory assumes the native XML item looks exactly like the <record> element
30
 * of an OAI GetRecord response, with the possible exception that the <metadata>
31
 * element contains multiple metadataFormats from which to choose.
32
 */
33
public class MetacatRecordFactory extends RecordFactory {
34

    
35
  private static final Logger logger = 
36
                                   Logger.getLogger(MetacatRecordFactory.class);
37

    
38
  private String repositoryIdentifier = null;
39
  private String context = null;
40
  private final String TEST_CONTEXT = "knb";
41

    
42

    
43
  /**
44
   * Construct a MetacatRecordFactory capable of producing the Crosswalk(s)
45
   * specified in the properties file.
46
   * 
47
   * @param properties
48
   *          Contains information to configure the factory: specifically, the
49
   *          names of the crosswalk(s) supported
50
   * @exception IllegalArgumentException
51
   *              Something is wrong with the argument.
52
   */
53
  public MetacatRecordFactory(Properties properties)
54
      throws IllegalArgumentException {
55
    super(properties);
56
    
57
    if (OAIHandler.isIntegratedWithMetacat()) {
58
      try {
59
        context = PropertyService.getProperty("application.context");
60
      }
61
      catch (PropertyNotFoundException e) {
62
        logger.error("PropertyNotFoundException: " + 
63
                     "unable to determine application.context value");
64
      }
65
    }
66
    else {
67
      context = TEST_CONTEXT;
68
    }
69
    
70
    repositoryIdentifier=properties.getProperty("oaipmh.repositoryIdentifier");
71
    if (repositoryIdentifier == null) {
72
      String errorStr = 
73
              "oaipmh.repositoryIdentifier is missing from the properties file";
74
      throw new IllegalArgumentException(errorStr);
75
    }
76
  }
77

    
78

    
79
  /**
80
   * Utility method to parse the 'local identifier' from the OAI identifier
81
   * 
82
   * @param identifier
83
   *          OAI identifier (e.g.
84
   *          "http://metacat.lternet.edu/knb/metacat/knb-lter-gce.247")
85
   * 
86
   * @return local identifier (e.g. "knb-lter-gce.247")
87
   */
88
  public String fromOAIIdentifier(String identifier) {
89
    try {
90
      if (identifier != null) {
91
        int i = identifier.indexOf("/metacat/");
92
        String tailString = identifier.substring(i + 9);
93
        StringTokenizer tokenizer = new StringTokenizer(tailString, "/");
94
        String localIdentifier = tokenizer.nextToken();
95
        return localIdentifier;
96
      } else {
97
        return null;
98
      }
99
    } catch (Exception e) {
100
      return null;
101
    }
102
  }
103

    
104

    
105
  /**
106
   * Construct an OAI identifier from the native item
107
   * 
108
   * @param nativeItem
109
   *          native Item object
110
   * @return OAI identifier
111
   */
112
  public String getOAIIdentifier(Object nativeItem) {
113
    String localIdentifier = getLocalIdentifier(nativeItem);
114
    StringBuffer sb = new StringBuffer();
115
    
116
    sb.append("http://");
117
    sb.append(repositoryIdentifier);
118
    sb.append("/" + context + "/metacat/");
119
    sb.append(localIdentifier);
120
    
121
    return sb.toString();
122
  }
123

    
124

    
125
  /**
126
   * Extract the local identifier from the native item
127
   * 
128
   * @param nativeItem
129
   *          native Item object
130
   * @return local identifier
131
   */
132
  public String getLocalIdentifier(Object nativeItem) {
133
    HashMap nativeItemMap = (HashMap) nativeItem;
134
    String localIdentifier = (String) nativeItemMap.get("localIdentifier");
135
    return localIdentifier;
136
  }
137

    
138

    
139
  /**
140
   * get the datestamp from the item
141
   * 
142
   * @param nativeItem
143
   *          a native item presumably containing a datestamp somewhere
144
   * @return a String containing the datestamp for the item
145
   * @exception IllegalArgumentException
146
   *              Something is wrong with the argument.
147
   */
148
  public String getDatestamp(Object nativeItem) 
149
      throws IllegalArgumentException {
150
    return (String) ((HashMap) nativeItem).get("lastModified");
151
  }
152

    
153

    
154
  /**
155
   * get the setspec from the item
156
   * 
157
   * @param nativeItem
158
   *          a native item presumably containing a setspec somewhere
159
   * @return a String containing the setspec for the item
160
   * @exception IllegalArgumentException
161
   *              Something is wrong with the argument.
162
   */
163
  public Iterator getSetSpecs(Object nativeItem)
164
      throws IllegalArgumentException {
165
    return null;
166
  }
167

    
168

    
169
  /**
170
   * Get the about elements from the item
171
   * 
172
   * @param nativeItem
173
   *          a native item presumably containing about information somewhere
174
   * @return a Iterator of Strings containing &lt;about&gt;s for the item
175
   * @exception IllegalArgumentException
176
   *              Something is wrong with the argument.
177
   */
178
  public Iterator getAbouts(Object nativeItem) throws IllegalArgumentException {
179
    return null;
180
  }
181

    
182

    
183
  /**
184
   * Is the record deleted?
185
   * 
186
   * @param nativeItem
187
   *          a native item presumably containing a possible delete indicator
188
   * @return true if record is deleted, false if not
189
   * @exception IllegalArgumentException
190
   *              Something is wrong with the argument.
191
   */
192
  public boolean isDeleted(Object nativeItem) throws IllegalArgumentException {
193
    return false;
194
  }
195

    
196

    
197
  /**
198
   * Allows classes that implement RecordFactory to override the default
199
   * create() method. This is useful, for example, if the entire &lt;record&gt;
200
   * is already packaged as the native record. Return null if you want the
201
   * default handler to create it by calling the methods above individually.
202
   * 
203
   * @param nativeItem
204
   *          the native record
205
   * @param schemaURL
206
   *          the schemaURL desired for the response
207
   * @param the
208
   *          metadataPrefix from the request
209
   * @return a String containing the OAI &lt;record&gt; or null if the default
210
   *         method should be used.
211
   */
212
  public String quickCreate(Object nativeItem, String schemaLocation,
213
      String metadataPrefix) {
214
    // Don't perform quick creates
215
    return null;
216
  }
217
  
218
  
219
  /**
220
   * Convert a native "item" to a "record" String. Use this version of
221
   * createHeader if the setSpecs are derived from the nativeItem itself.
222
   * 
223
   * @param nativeItem
224
   *          the native record.
225
   * @return String[0] = "header" XML string String[1] = oai-identifier.
226
   * @exception IllegalArgumentException
227
   *              One of the header components for this record is bad.
228
   */
229
  public String[] createHeader(Object nativeItem)
230
      throws IllegalArgumentException {
231
    String oaiIdentifier = getOAIIdentifier(nativeItem);
232
    String datestamp = getDatestamp(nativeItem);
233
    Iterator setSpecs = getSetSpecs(nativeItem);
234
    boolean isDeleted = isDeleted(nativeItem);
235
    
236
    String[] headerArray = 
237
                    createHeader(oaiIdentifier, datestamp, setSpecs, isDeleted);
238
    return headerArray;
239
  }
240
  
241
}
(2-2/2)