Project

General

Profile

« Previous | Next » 

Revision 6359

-handle NotFound object format when generating system metadata
-improve EML DML for null entity/attribute elements

View differences:

src/edu/ucsb/nceas/metacat/util/MetacatPopulator.java
45 45
import org.dataone.client.MNode;
46 46
import org.dataone.client.ObjectFormatCache;
47 47
import org.dataone.client.auth.CertificateManager;
48
import org.dataone.service.exceptions.NotFound;
48 49
import org.dataone.service.types.AccessPolicy;
49 50
import org.dataone.service.types.AccessRule;
50 51
import org.dataone.service.types.Checksum;
......
165 166

  
166 167
            printHeader("creating document on destination " + destinationUrl);            
167 168
            SystemMetadata sysmeta = generateSystemMetadata(doc);
168
            for(int j=0; j < dataPackage.getEntityList().length; j++)
169
            {
170
                String dataDocUrl = dataPackage.getEntityList()[j].getURL();
171
                String dataDocMimeType = 
172
                	dataPackage.getEntityList()[j].getDataFormat();
173
                if (dataDocMimeType == null) {
174
	                dataDocMimeType = 
175
	                	ObjectFormatCache.getInstance().getFormat("application/octet-stream").getFmtid().getValue();
176
                }
177
                String dataDocLocalId = "";
178
                if(dataDocUrl.trim().startsWith("ecogrid://knb/"))
179
                { //we only handle ecogrid urls right now
180
                    dataDocLocalId = dataDocUrl.substring(dataDocUrl.indexOf("ecogrid://knb/") + 
181
                            "ecogrid://knb/".length(), dataDocUrl.length());
182
                    //get the file
183
                    params = "action=read&qformat=xml&docid=" + dataDocLocalId;
184
                    InputStream dataDocIs = getResponse(sourceUrl, "/metacat", params, "POST");
185
                    String dataDocText = streamToString(dataDocIs);
186
                    
187
                    //set the id
188
                    Identifier did = new Identifier();
189
                    did.setValue(dataDocLocalId);
190
                    
191
                    //add the desribeby to the eml's sysmeta
192
                    System.out.println("adding describe for doc " + 
193
                            sysmeta.getIdentifier().getValue() + " :" + did.getValue());
194
                    sysmeta.addDescribe(did);
195
                    
196
                    //create sysmeta for the data doc                    
197
                    SystemMetadata dataDocSysMeta = generateSystemMetadata(doc);
198
                    //overwrite the bogus values from the last call 
199
                    dataDocSysMeta.setIdentifier(did);
200
                    dataDocSysMeta.setObjectFormat(ObjectFormatCache.getInstance().getFormat(dataDocMimeType));
201
                    Checksum checksum = new Checksum();
202
                    dataDocIs = stringToStream(dataDocText);
203
                    ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
204
                    checksum.setAlgorithm(ca);
205
                    checksum.setValue(checksum(dataDocIs));
206
                    dataDocSysMeta.setChecksum(checksum);
207
                    dataDocSysMeta.setSize(dataDocText.getBytes(MetaCatServlet.DEFAULT_ENCODING).length);
208
                    dataDocSysMeta.addDescribedBy(sysmeta.getIdentifier());
209
                    boolean error = false;
210
                    
211
                    // create access policy
212
                    //"public", "read", "allow", "allowFirst"
213
                    AccessPolicy accessPolicy = new AccessPolicy();
214
                    AccessRule accessRule = new AccessRule();
215
					accessRule.addPermission(Permission.READ);
216
                    Subject subject = new Subject();
217
                    subject.setValue("public");
218
					accessRule.addSubject(subject );
219
					accessPolicy.addAllow(accessRule );
220
                    //create the data doc on d1
221
                    try
222
                    {
223
                        mn.create(session, dataDocSysMeta.getIdentifier(), IOUtils.toInputStream(dataDocText), dataDocSysMeta);
224
						mn.setAccessPolicy(session, dataDocSysMeta.getIdentifier(), accessPolicy);
225
                    }
226
                    catch(Exception e)
227
                    {
228
                        error = true;
229
                        System.out.println("ERROR: Could not create data document with id " + 
230
                                dataDocSysMeta.getIdentifier().getValue() + " : " + e.getMessage());
231
                    }
232
                    finally
233
                    {
234
                        if(error)
235
                        {
236
                            printHeader("Insertion of document " + dataDocSysMeta.getIdentifier().getValue() + 
237
                                    "FAILED.");
238
                        }
239
                        else
240
                        {
241
                            printHeader("Done inserting document " + dataDocSysMeta.getIdentifier().getValue() +
242
                                " which is described by " + sysmeta.getIdentifier().getValue());
243
                        }
244
                    }
245
                }
246
                else
247
                {
248
                    System.out.println("WARNING: Could not process describes url " +
249
                            dataDocUrl + " for document " + doc.docid + 
250
                    ".  Only ecogrid://knb/ urls are currently supported.");
251
                }
169
            if (dataPackage.getEntityList() != null) {
170
	            for(int j=0; j < dataPackage.getEntityList().length; j++)
171
	            {
172
	                String dataDocUrl = dataPackage.getEntityList()[j].getURL();
173
	                String dataDocMimeType = 
174
	                	dataPackage.getEntityList()[j].getDataFormat();
175
	                if (dataDocMimeType == null) {
176
		                dataDocMimeType = 
177
		                	ObjectFormatCache.getInstance().getFormat("application/octet-stream").getFmtid().getValue();
178
	                }
179
	                String dataDocLocalId = "";
180
	                if(dataDocUrl.trim().startsWith("ecogrid://knb/"))
181
	                { //we only handle ecogrid urls right now
182
	                    dataDocLocalId = dataDocUrl.substring(dataDocUrl.indexOf("ecogrid://knb/") + 
183
	                            "ecogrid://knb/".length(), dataDocUrl.length());
184
	                    //get the file
185
	                    params = "action=read&qformat=xml&docid=" + dataDocLocalId;
186
	                    InputStream dataDocIs = getResponse(sourceUrl, "/metacat", params, "POST");
187
	                    String dataDocText = streamToString(dataDocIs);
188
	                    
189
	                    //set the id
190
	                    Identifier did = new Identifier();
191
	                    did.setValue(dataDocLocalId);
192
	                    
193
	                    //add the desribeby to the eml's sysmeta
194
	                    System.out.println("adding describe for doc " + 
195
	                            sysmeta.getIdentifier().getValue() + " :" + did.getValue());
196
	                    sysmeta.addDescribe(did);
197
	                    
198
	                    //create sysmeta for the data doc                    
199
	                    SystemMetadata dataDocSysMeta = generateSystemMetadata(doc);
200
	                    //overwrite the bogus values from the last call 
201
	                    dataDocSysMeta.setIdentifier(did);
202
	                    ObjectFormat format = null;
203
	                    try {
204
	                    	format = ObjectFormatCache.getInstance().getFormat(dataDocMimeType);
205
	                    } catch (NotFound e) {
206
							System.out.println(e.getMessage());
207
						}
208
						dataDocSysMeta.setObjectFormat(format);
209
	                    Checksum checksum = new Checksum();
210
	                    dataDocIs = stringToStream(dataDocText);
211
	                    ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
212
	                    checksum.setAlgorithm(ca);
213
	                    checksum.setValue(checksum(dataDocIs));
214
	                    dataDocSysMeta.setChecksum(checksum);
215
	                    dataDocSysMeta.setSize(dataDocText.getBytes(MetaCatServlet.DEFAULT_ENCODING).length);
216
	                    dataDocSysMeta.addDescribedBy(sysmeta.getIdentifier());
217
	                    boolean error = false;
218
	                    
219
	                    // create access policy
220
	                    //"public", "read", "allow", "allowFirst"
221
	                    AccessPolicy accessPolicy = new AccessPolicy();
222
	                    AccessRule accessRule = new AccessRule();
223
						accessRule.addPermission(Permission.READ);
224
	                    Subject subject = new Subject();
225
	                    subject.setValue("public");
226
						accessRule.addSubject(subject );
227
						accessPolicy.addAllow(accessRule );
228
	                    //create the data doc on d1
229
	                    try
230
	                    {
231
	                        mn.create(session, dataDocSysMeta.getIdentifier(), IOUtils.toInputStream(dataDocText), dataDocSysMeta);
232
							mn.setAccessPolicy(session, dataDocSysMeta.getIdentifier(), accessPolicy);
233
	                    }
234
	                    catch(Exception e)
235
	                    {
236
	                        error = true;
237
	                        System.out.println("ERROR: Could not create data document with id " + 
238
	                                dataDocSysMeta.getIdentifier().getValue() + " : " + e.getMessage());
239
	                    }
240
	                    finally
241
	                    {
242
	                        if(error)
243
	                        {
244
	                            printHeader("Insertion of document " + dataDocSysMeta.getIdentifier().getValue() + 
245
	                                    "FAILED.");
246
	                        }
247
	                        else
248
	                        {
249
	                            printHeader("Done inserting document " + dataDocSysMeta.getIdentifier().getValue() +
250
	                                " which is described by " + sysmeta.getIdentifier().getValue());
251
	                        }
252
	                    }
253
	                }
254
	                else
255
	                {
256
	                    System.out.println("WARNING: Could not process describes url " +
257
	                            dataDocUrl + " for document " + doc.docid + 
258
	                    ".  Only ecogrid://knb/ urls are currently supported.");
259
	                }
260
	            }
252 261
            }
253 262
            
254 263
            try

Also available in: Unified diff