Project

General

Profile

« Previous | Next » 

Revision 3372

Added by barteau over 17 years ago

Changes for java 1.4 compilation, and further development.

View differences:

src/edu/ucsb/nceas/metacat/clientview/ClientFgdcHelper.java
41 41
     * Data Document ID location within an FGDC document.  XPath expression.
42 42
     */
43 43
    public static final String              FGDC_DATA_FILE_DOCID_XPATH = "/metadata/distinfo/stdorder/digform/digtopt/onlinopt/computer/networka/networkr";
44
    public static final String              FGDC_DATA_FILE_QUERY_XPATH = FGDC_DATA_FILE_DOCID_XPATH + "[text()='%1s']";
44 45
    
45 46
    public static final String              PATH4ANCESTOR = FGDC_DATA_FILE_DOCID_XPATH + "[text()='%1s']/ancestor::node()[name()='%2s']";
46 47
    
......
49 50
     */
50 51
    public static final String              FGDC_DOCID_XPATH = "/metadata/distinfo/resdesc";
51 52
    
53
    /**
54
     * Metadata Document ID query template within an FGDC document.  Substitute %1s with Doc Id.
55
     */
52 56
    public static final String              XPATH_QUERY_TEMPLATE = FGDC_DOCID_XPATH + "[text()='%1s']";
53 57
    
54 58
    /**
......
137 141
        return(revision);
138 142
    }
139 143
    
140
    public static boolean handlePackageUpload(String metaDocId, 
141
            HashMap dataDocIDs, 
142
            String contactName, 
143
            String metaFNm, 
144
    public static boolean handlePackageUpload(String metaDocId,
145
            HashMap dataDocIDs,
146
            String contactName,
147
            String metaFNm,
144 148
            Document metadataDoc) throws IOException {
145 149
        boolean                         result = true;
146 150
        Node                            newBranch, metaRootNode;
......
185 189
        boolean                     result = false;
186 190
        String                      queryResult, xPathQuery;
187 191
        
188
        xPathQuery = String.format(XPATH_QUERY_TEMPLATE, docId);
192
        //xPathQuery = String.format(XPATH_QUERY_TEMPLATE, docId);
193
        xPathQuery = XPATH_QUERY_TEMPLATE.replaceFirst("%1s", docId);
189 194
        try {
190 195
            queryResult = xpath.evaluate(xPathQuery, metadataDoc);
191 196
            result = (queryResult != null && !queryResult.equals(""));
......
315 320
        }
316 321
        return(result);
317 322
    }
323
    
324
    public static void updateFileNameAndType(Node root, String dataDocId, String[] fileInfo) {
325
        Node                digform;
326
        String              tmp, pathToDigform;
327
        final String        FORMNAME_PATH = "digtinfo/formname";  //*** File format type.
328
        final String        FORMCONT_PATH = "digtinfo/formcont";  //*** Original file name.
329
        
330
        tmp = PATH4ANCESTOR.replaceFirst("%1s", dataDocId);
331
        pathToDigform = tmp.replaceFirst("%2s", "digform");
332
        digform = ClientViewHelper.getNode(xpath, pathToDigform, root);
333
        if (digform != null) {
334
            ClientViewHelper.updateNodeText(digform, xpath, FORMNAME_PATH, fileInfo[ClientView.FORMAT_TYPE]);
335
            ClientViewHelper.updateNodeText(digform, xpath, FORMCONT_PATH, fileInfo[ClientView.FILE_NAME]);
336
        }
337
    }
338
    
339
    public static void updateMetadataFileName(Node root, String metadataDocId, String fileName) {
340
        String                  pathToResdesc;
341
        Node                    resdesc;
342
        
343
        if (fileName != null && !fileName.equals("")) {
344
            pathToResdesc = XPATH_QUERY_TEMPLATE.replaceFirst("%1s", metadataDocId);
345
            ClientViewHelper.updateNodeText(root, xpath, pathToResdesc, fileName);
346
        }
347
    }
318 348
}
src/edu/ucsb/nceas/metacat/clientview/ClientViewHelper.java
120 120
                    message = handleDocIdSelect();
121 121
                    clientViewBean.setMessage(ClientView.SELECT_MESSAGE, message);
122 122
                    
123
                } else if (action.equals("Replace")) {
124
                    message = "Not implemented, come back another day";
125
                    clientViewBean.setMessage(ClientView.REPLACE_MESSAGE, message);
126
                    
127 123
                } else if (action.equals("Upload")) {
128 124
                    message = "";
129 125
                    //*** Only process request if a file upload.
......
135 131
                    clientViewBean.setMessage(ClientView.UPLOAD_MESSAGE, message);
136 132
                    
137 133
                } else if (action.equals("Update")) {
138
                    System.out.println("ClientViewHelper.clientRequest: Update");
134
                    System.out.println("This is not implemented here.  Call ClientViewHelper.jspx");
139 135
                } else if (action.equals("Scope")) {
140 136
                    message = handleDocIdSelect();
141 137
                    clientViewBean.setMessage(ClientView.SELECT_MESSAGE, message);
......
265 261
                //*** Get the data file's DOC ID.
266 262
                nextDocId = nextDocId(lastDocId, clientViewBean.getUsername());
267 263
                
268
                //*** Set the file format (just using file extension for now).
269
                idx = fileName.lastIndexOf(".");
270
                if (idx > 1)
271
                    formatType = fileName.substring(idx+1).toUpperCase();
272
                else
273
                    formatType = "";
274
                
275
                fileInfo = new String[2];
276
                fileInfo[ClientView.FORMAT_TYPE] = formatType;
277
                fileInfo[ClientView.FILE_NAME] = fileName.toString();
278
                
264
                fileInfo = parseFileInfo(fileName.toString());
279 265
                dataDocIDs.put(nextDocId, fileInfo);
280 266
                //*** Upload the data file to metacat.
281 267
                getMetacatClient().upload(nextDocId, fileName.toString(), inputStream, Integer.MAX_VALUE);
......
315 301
    }
316 302
    
317 303
    public String handleFileUpdate(MultipartParser multipartParser) throws Exception {
318
        Reader                      reader;
319 304
        String                      result = "", fNm, action, lastDocId, newDocId, xPathQuery;
320 305
        InputStream                 inputStream;
321 306
        HashMap                     paramsMap;
322 307
        StringBuilder               fileName;
323 308
        Iterator                    iterIt;
324 309
        boolean                     sendIt;
310
        String                      metadataDocId, fileInfo[];
325 311
        
326 312
        paramsMap = new HashMap();
327 313
        fileName = new StringBuilder();
328 314
        if ((inputStream = getNextInputStream(multipartParser, fileName, paramsMap)) != null) {
329 315
            action = (String) paramsMap.get("action");
316
            //*** Get the Doc Id.
330 317
            lastDocId = (String) paramsMap.get("docid");
318
            //*** Get the metadata Doc Id.
319
            metadataDocId = (String) paramsMap.get("metadataDocId");
331 320
            fNm = fileName.toString();
321
            
332 322
            try {
333
                if (fNm.toLowerCase().endsWith(".xml") && action.toLowerCase().contains("metadata")) {
323
                if (lastDocId.equals(metadataDocId)) { //*** This is the metadata file.
334 324
                    //*** Keep it here for updating.
335 325
                    setMetadataDoc(inputStream);
336 326
                    if (ClientFgdcHelper.isFGDC(getMetadataDoc())) {
327
                        clientViewBean.setContentStandard(ClientView.FEDERAL_GEOGRAPHIC_DATA_COMMITTEE);
337 328
                        if (!ClientFgdcHelper.hasMetacatInfo(lastDocId, getMetadataDoc())) {
338 329
                            result = "Update not performed: the Metadata file has no prior Metacat info in it.";
339 330
                        } else {
340
                            //xPathQuery = String.format(ClientFgdcHelper.XPATH_QUERY_TEMPLATE, lastDocId);
341 331
                            xPathQuery = ClientFgdcHelper.XPATH_QUERY_TEMPLATE.replaceFirst("%1s", lastDocId);
342
                            //*** Update the metadata with the new Doc Id version.
343
                            newDocId = nextVersion(lastDocId, xPathQuery);
344
                            //*** Upload the metadata file to metacat.
345
                            reader = XMLUtilities.getDOMTreeAsReader(getMetadataDoc().getDocumentElement(), false);
346
                            getMetacatClient().update(newDocId, reader, null);
347
                            reader.close();
332
                            newDocId = updateMetadataDoc(lastDocId, xPathQuery, fNm);
348 333
                            result = "MetaCat Package Updated:  the Document Identifier is " + newDocId;
349 334
                        }
350 335
                    } else {
351 336
                        //***TODO This is EML.
337
                        clientViewBean.setContentStandard(ClientView.ECOLOGICAL_METADATA_LANGUAGE);
352 338
                        result = "Currently this functionality only supports FGDC metadata.";
353 339
                    }
354 340
                } else {
355 341
                    //*** This is a data file.
356
                    result = "This is a data file.  action: " + action + "  lastDocId: " + lastDocId + "  fNm: " + fNm;
342
                    //*** Query for the metadata, we need to update it with the new data file doc id.
343
                    setMetadataDoc(metadataDocId);
344
                    if (ClientFgdcHelper.isFGDC(getMetadataDoc())) {
345
                        clientViewBean.setContentStandard(ClientView.FEDERAL_GEOGRAPHIC_DATA_COMMITTEE);
346
                        fileInfo = parseFileInfo(fNm);
347
                        
348
                        xPathQuery = ClientFgdcHelper.FGDC_DATA_FILE_QUERY_XPATH.replaceFirst("%1s", lastDocId);
349
                        newDocId = nextVersion(lastDocId, xPathQuery);
350
                        ClientFgdcHelper.updateFileNameAndType(getMetadataDoc().getDocumentElement(), newDocId, fileInfo);
351
                        //*** Upload the data file to metacat.
352
                        //result = getMetacatClient().upload(newDocId, fNm, inputStream, Integer.MAX_VALUE);
353
                        result = "Updated " + lastDocId + " with " + fNm + " (" + newDocId + ") ";
354
                        
355
                        //*** Upload the metadata file to metacat.
356
                        xPathQuery = ClientFgdcHelper.XPATH_QUERY_TEMPLATE.replaceFirst("%1s", lastDocId);
357
                        newDocId = updateMetadataDoc(metadataDocId, xPathQuery, null);
358
                        System.out.println("ClientViewHelper.handleFileUpdate:\n" + XMLUtilities.getDOMTreeAsString(getMetadataDoc().getDocumentElement()));
359
                    } else {
360
                        //***TODO This is EML.
361
                        clientViewBean.setContentStandard(ClientView.ECOLOGICAL_METADATA_LANGUAGE);
362
                        result = "Currently this functionality only supports FGDC metadata.";
363
                    }
357 364
                    
358
                    //newDocId = "TODO";
359
                    //reader = null; //TODO
360
                    //getMetacatClient().insert(newDocId, reader, null);
361
                    //reader.close();
362 365
                }
363
                
364
                
365
                //*** Pass on the public read access. TODO
366
//                        if(hasPublicAccess)
367
//                        getMetacatClient().setAccess(newDocId, "public", "read", "allow", "allowFirst");
368 366
            } catch (java.io.IOException ex) {
369 367
                ex.printStackTrace();
370 368
            }
371
            
372
            //DEBUG STUFF
373
            //String                      key, val;
374
            //result = "-----------------iteration------------<br/>fNm = " + fNm + "<br/>";
375
            //iterIt = paramsMap.keySet().iterator();
376
            //while(iterIt.hasNext()) {
377
            //    key = (String) iterIt.next();
378
            //    val = (String) paramsMap.get(key);
379
            //    result += key + ": " + val + "<br/>";
380
            //}
381
            
382 369
        } else {
383 370
            result = "Please enter the updated file path/name.";
384 371
        }
385 372
        return(result);
386 373
    }
387 374
    
375
    private String updateMetadataDoc(String lastDocId, String docIdPath, String origFileName) {
376
        String                      newDocId = null;
377
        Reader                      reader;
378
        
379
        //*** Update the metadata with the new Doc Id version.
380
        try {
381
            newDocId = nextVersion(lastDocId, docIdPath);
382
            if (origFileName != null) {
383
                if (clientViewBean.getContentStandard().equals(ClientView.FEDERAL_GEOGRAPHIC_DATA_COMMITTEE))
384
                    ClientFgdcHelper.updateMetadataFileName(getMetadataDoc().getDocumentElement(), newDocId, origFileName);
385
                else
386
                    ; //TODO EML, etc.
387
            }
388
            //*** Upload the metadata file to metacat.
389
            //reader = XMLUtilities.getDOMTreeAsReader(getMetadataDoc().getDocumentElement(), false);
390
            //getMetacatClient().update(newDocId, reader, null);
391
            //reader.close();
392
        } catch (Exception ex) {
393
            ex.printStackTrace();
394
        }
395
        return(newDocId);
396
    }
397
    
388 398
    private InputStream getNextInputStream(MultipartParser multipartParser, StringBuilder fileName, HashMap paramsMap)
389 399
    throws IOException {
390 400
        InputStream                     result = null;
......
644 654
    public MetacatClient getMetacatClient() {
645 655
        return(metacatClient);
646 656
    }
657
    
658
    //*** BEGIN: Static utility methods ***
659
    
660
    public static String[] parseFileInfo(String fileName) {
661
        String[]                        result = new String[2];
662
        int                             idx;
663
        String                          formatType;
664
        
665
        //*** Set the file format (just using file extension for now).
666
        idx = fileName.lastIndexOf(".");
667
        if (idx > 1)
668
            formatType = fileName.substring(idx+1).toUpperCase();
669
        else
670
            formatType = "";
671
        
672
        result[ClientView.FORMAT_TYPE] = formatType;
673
        result[ClientView.FILE_NAME] = fileName.toString();
674
        return(result);
675
    }
676
    
677
    public static void updateNodeText(Node root, XPath xPath, String expression, String text) {
678
        Node                    targetNode;
679
        
680
        if (text != null && !text.equals("")) {
681
            try {
682
                targetNode = (Node) xPath.evaluate(expression, root, XPathConstants.NODE);
683
                targetNode.setTextContent(text);
684
            } catch (XPathExpressionException ex) {
685
                ex.printStackTrace();
686
            }
687
        }
688
    }
689
    
690
    
691
    public static Node getNode(XPath xPath, String expression, Node root) {
692
        Node                        result = null;
693
        
694
        try {
695
            result = (Node) xPath.evaluate(expression, root, XPathConstants.NODE);
696
        } catch (XPathExpressionException ex) {
697
            ex.printStackTrace();
698
        }
699
        return(result);
700
        
701
    }
702
    //*** END: Static utility methods ***
703
    
647 704
}

Also available in: Unified diff