Project

General

Profile

« Previous | Next » 

Revision 3364

Added by barteau over 17 years ago

Replaced "Formatter" usage (java 1.5) for java 1.4 compiling.

View differences:

src/edu/ucsb/nceas/metacat/clientview/ClientViewHelper.java
25 25
import java.io.Reader;
26 26
import java.io.StringReader;
27 27
import java.util.HashMap;
28
import java.util.Iterator;
28 29
import java.util.Properties;
29 30
import java.util.TreeMap;
30 31
import java.util.Vector;
......
60 61
     */
61 62
    public ClientViewHelper(HttpServletRequest request) throws MetacatInaccessibleException {
62 63
        String                              metacatPath = "http://%1$s%2$s/metacat";
63
        String                              host, context, button;
64
        String                              host, context, button, tmp;
64 65
        
65 66
        clientSession = request.getSession();
66 67
        
67 68
        host = request.getHeader("host");
68 69
        context = request.getContextPath();
69
        metacatPath = String.format(metacatPath, host, context);
70
        //metacatPath = String.format(metacatPath, host, context);
71
        tmp = metacatPath.replaceFirst("%1$s", host);
72
        metacatPath = tmp.replaceFirst("%2$s", context);
70 73
        metacatClient = (MetacatClient) MetacatFactory.createMetacatConnection(metacatPath);
71 74
        sizeLimit = (new Integer(MetaCatUtil.getOption("datafilesizelimit"))).intValue();
72 75
    }
73 76
    
74 77
    public String clientRequest(HttpServletRequest request, HttpServletResponse response)  {
75 78
        String                              result = null, action, message, serverResponse;
76
        String                              button, contentType, posted_ldapUserName;
79
        String                              button, contentType, posted_ldapUserName, tmp;
77 80
        MultipartParser                     multipartParser;
78 81
        
79 82
        if (clientViewBean == null)
......
86 89
                action = "Upload";
87 90
            try {
88 91
                if (action.equals("Login")) {
89
                    posted_ldapUserName = String.format(LDAP_TEMPLATE, clientViewBean.getUsername(), clientViewBean.getOrganization());
92
                    //posted_ldapUserName = String.format(LDAP_TEMPLATE, clientViewBean.getUsername(), clientViewBean.getOrganization());
93
                    tmp = LDAP_TEMPLATE.replaceFirst("%1$s", clientViewBean.getUsername());
94
                    posted_ldapUserName = tmp.replaceFirst("%2$s", clientViewBean.getOrganization());
95
                    
90 96
                    serverResponse = metacatClient.login(posted_ldapUserName, clientViewBean.getPassword());
91 97
                    setLoggedIn(serverResponse);
92 98
                    message = parseXml("message", serverResponse);
......
129 135
                    clientViewBean.setMessage(ClientView.UPLOAD_MESSAGE, message);
130 136
                    
131 137
                } else if (action.equals("Update")) {
132
                    
138
                    System.out.println("ClientViewHelper.clientRequest: Update");
133 139
                } else if (action.equals("Scope")) {
134 140
                    message = handleDocIdSelect();
135 141
                    clientViewBean.setMessage(ClientView.SELECT_MESSAGE, message);
......
194 200
        loggedIn = (serverResponse != null && serverResponse.contains("login"));
195 201
    }
196 202
    
197
    private String parseXml(String elementName, String xml) {
203
    public String parseXml(String elementName, String xml) {
198 204
        String                      result = null;
199 205
        Document                    doc;
200 206
        
......
211 217
        return(result);
212 218
    }
213 219
    
214
    private String handleDocIdSelect() {
220
    public String handleDocIdSelect() {
215 221
        String                              result = "";
216 222
        TreeMap                             allDocIds;
217 223
        
......
229 235
     * @param request HTTP request.
230 236
     * @return A 1-line status message for the user.
231 237
     */
232
    private String handlePackageUpload(ClientView clientViewBean, MultipartParser multipartParser) throws Exception {
238
    public String handlePackageUpload(ClientView clientViewBean, MultipartParser multipartParser) throws Exception {
233 239
        String                      result = "", contentType, formatType;
234
        String                      lastDocId, nextDocId, metaDocId;
240
        String                      lastDocId, nextDocId, metaDocId, metaFileName;
241
        String                      fileInfo[];
235 242
        Reader                      reader;
236 243
        int                         sizeLimit, idx;
237 244
        InputStream                 inputStream;
......
243 250
        paramsMap = new HashMap();
244 251
        fileName = new StringBuilder();
245 252
        inputStream = getNextInputStream(multipartParser, fileName, paramsMap);
246
        if (fileName.toString().toLowerCase().endsWith(".xml")) {
253
        metaFileName = fileName.toString();
254
        if (metaFileName.toLowerCase().endsWith(".xml")) {
247 255
            //*** Keep it here for updating.
248 256
            setMetadataDoc(inputStream);
249
            
250 257
            //*** Get the Metadata File's DOC ID.
251 258
            lastDocId = getMetacatClient().getLastDocid(clientViewBean.getUsername());
252 259
            metaDocId = lastDocId = nextDocId(lastDocId, clientViewBean.getUsername());
253 260
            
254 261
            //*** Loop thru all of the data files, get fileName and inputStream.
255 262
            dataDocIDs = new HashMap();
263
            fileName = new StringBuilder();
256 264
            while ((inputStream = getNextInputStream(multipartParser, fileName, paramsMap)) != null) {
257 265
                //*** Get the data file's DOC ID.
258 266
                nextDocId = nextDocId(lastDocId, clientViewBean.getUsername());
......
263 271
                    formatType = fileName.substring(idx+1).toUpperCase();
264 272
                else
265 273
                    formatType = "";
266
                dataDocIDs.put(nextDocId, formatType);
274
                
275
                fileInfo = new String[2];
276
                fileInfo[ClientView.FORMAT_TYPE] = formatType;
277
                fileInfo[ClientView.FILE_NAME] = fileName.toString();
278
                
279
                dataDocIDs.put(nextDocId, fileInfo);
267 280
                //*** Upload the data file to metacat.
268 281
                getMetacatClient().upload(nextDocId, fileName.toString(), inputStream, Integer.MAX_VALUE);
269 282
                
......
272 285
            }
273 286
            
274 287
            if (ClientFgdcHelper.isFGDC(getMetadataDoc())) {
275
                sendIt = ClientFgdcHelper.handlePackageUpload(metaDocId, dataDocIDs, contactName, getMetadataDoc());
288
                sendIt = ClientFgdcHelper.handlePackageUpload(metaDocId, dataDocIDs, contactName, metaFileName, getMetadataDoc());
276 289
            } else {
277 290
                //TODO add other types of metadata grammars here...
278 291
                System.out.println("ClientViewHelper.handlePackageUpload: not an FGDC file = " + fileName);
......
301 314
        return(result);
302 315
    }
303 316
    
317
    public String handleFileUpdate(MultipartParser multipartParser) throws Exception {
318
        Reader                      reader;
319
        String                      result = "", fNm, action, lastDocId, newDocId, xPathQuery;
320
        InputStream                 inputStream;
321
        HashMap                     paramsMap;
322
        StringBuilder               fileName;
323
        Iterator                    iterIt;
324
        boolean                     sendIt;
325
        
326
        paramsMap = new HashMap();
327
        fileName = new StringBuilder();
328
        if ((inputStream = getNextInputStream(multipartParser, fileName, paramsMap)) != null) {
329
            action = (String) paramsMap.get("action");
330
            lastDocId = (String) paramsMap.get("docid");
331
            fNm = fileName.toString();
332
            try {
333
                if (fNm.toLowerCase().endsWith(".xml") && action.toLowerCase().contains("metadata")) {
334
                    //*** Keep it here for updating.
335
                    setMetadataDoc(inputStream);
336
                    if (ClientFgdcHelper.isFGDC(getMetadataDoc())) {
337
                        if (!ClientFgdcHelper.hasMetacatInfo(lastDocId, getMetadataDoc())) {
338
                            result = "Update not performed: the Metadata file has no prior Metacat info in it.";
339
                        } else {
340
                            //xPathQuery = String.format(ClientFgdcHelper.XPATH_QUERY_TEMPLATE, lastDocId);
341
                            xPathQuery = ClientFgdcHelper.XPATH_QUERY_TEMPLATE.replaceFirst("%1$s", 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();
348
                            result = "MetaCat Package Updated:  the Document Identifier is " + newDocId;
349
                        }
350
                    } else {
351
                        //***TODO This is EML.
352
                        result = "Currently this functionality only supports FGDC metadata.";
353
                    }
354
                } else {
355
                    //*** This is a data file.
356
                    result = "This is a data file.";
357
                    
358
                    //newDocId = "TODO";
359
                    //reader = null; //TODO
360
                    //getMetacatClient().insert(newDocId, reader, null);
361
                    //reader.close();
362
                }
363
                
364
                
365
                //*** Pass on the public read access. TODO
366
//                        if(hasPublicAccess)
367
//                        getMetacatClient().setAccess(newDocId, "public", "read", "allow", "allowFirst");
368
            } catch (java.io.IOException ex) {
369
                ex.printStackTrace();
370
            }
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
        } else {
383
            result = "Please enter the updated file path/name.";
384
        }
385
        return(result);
386
    }
304 387
    
305 388
    private InputStream getNextInputStream(MultipartParser multipartParser, StringBuilder fileName, HashMap paramsMap)
306 389
    throws IOException {
......
329 412
        return(result);
330 413
    }
331 414
    
415
    private void getRemainingParameters(MultipartParser multipartParser, HashMap paramsMap)
416
    throws IOException {
417
        InputStream                     result = null;
418
        Part                            part;
419
        String                          parmName = null, value = null, fnam;
420
        
421
        while ((part = multipartParser.readNextPart()) != null) {
422
            if (part.isParam()) {
423
                parmName = part.getName();
424
                value = ((ParamPart) part).getStringValue();
425
                paramsMap.put(parmName, value);
426
                System.out.println("ClientViewHelper.getNextInputStream: parmName = " + parmName + "  value = " + value);
427
            }
428
        }
429
    }
430
    
332 431
    /**
333 432
     * Queries Metacat for document listings, and returns the results in a TreeMap,
334 433
     * where the key is the Doc Id, and the value is the Create Date.  If the document
......
357 456
        value = clientViewBean.getPathValue();
358 457
        
359 458
        result = new TreeMap();
360
        paramExpr = String.format(PARAM_EXPR, returnFld);
361
        
459
        //paramExpr = String.format(PARAM_EXPR, returnFld);
460
        paramExpr = PARAM_EXPR.replaceFirst("%1$s", returnFld);
362 461
        //*** Query the database ***
363 462
        doc = query(path, value, returnFld);
364 463
        //*** Build the TreeMap to return ***
......
464 563
    }
465 564
    
466 565
    public String nextVersion(String lastDocId, String xPathQuery) throws XPathExpressionException {
467
        String                      result = null, tokens[], scope, ready2Split;
566
        String                      result = null, tokens[], scope, ready2Split, tmp;
468 567
        int                         vers, docNum;
469 568
        final int                   LAST_TOKEN = 2;
470 569
        final String                TEMPLATE = "%1$s.%2$d.%3$d";
......
479 578
                docNum = Integer.parseInt(tokens[LAST_TOKEN - 1]);
480 579
                try {
481 580
                    vers = Integer.parseInt(tokens[LAST_TOKEN]);
482
                    result = String.format(TEMPLATE, scope, docNum, 1 + vers);
581
                    //result = String.format(TEMPLATE, scope, docNum, 1 + vers);
582
                    tmp = TEMPLATE.replaceFirst("%1$s", scope);
583
                    tmp = tmp.replaceFirst("%2$d", String.valueOf(docNum));
584
                    result = tmp.replaceFirst("%3$d", String.valueOf(vers + 1));
585
                    
483 586
                } catch (NumberFormatException ex) {
484 587
                    //*** In case the lastDocId has something other than a number.
485
                    result = String.format(TEMPLATE, scope, docNum, 1);
588
                    //result = String.format(TEMPLATE, scope, docNum, 1);
589
                    tmp = TEMPLATE.replaceFirst("%1$s", scope);
590
                    tmp = tmp.replaceFirst("%2$d", String.valueOf(docNum));
591
                    result = tmp.replaceFirst("%3$d", "1");
486 592
                }
487 593
            } else {
488 594
                //*** In case the lastDocId ends with a '.'
......
501 607
    }
502 608
    
503 609
    private String nextDocId(String lastDocId, String scope) {
504
        String                      result = null, tokens[];
610
        String                      result = null, tokens[], tmp;
505 611
        int                         vers;
506 612
        String                      template = scope.toLowerCase() + ".%1$d.%2$d";
507 613
        
......
511 617
            if(tokens.length > 1 && !tokens[1].equals("")) {
512 618
                try {
513 619
                    vers = Integer.parseInt(tokens[1]);
514
                    result = String.format(template, 1 + vers, 1);
620
                    //result = String.format(template, 1 + vers, 1);
621
                    tmp = template.replaceFirst("%1$d", String.valueOf(1 + vers));
622
                    result = tmp.replaceFirst("%2$d", "1");
515 623
                } catch (NumberFormatException ex) {
516 624
                    //*** In case the lastDocId has something other than a number.
517
                    result = String.format(template, 1, 1);
625
                    //result = String.format(template, 1, 1);
626
                    tmp = template.replaceFirst("%1$d", "1");
627
                    result = tmp.replaceFirst("%2$d", "1");
518 628
                }
519 629
            } else {
520 630
                //*** In case the lastDocId ends with a '.'
521
                result = String.format(template, 1, 1);
631
                //result = String.format(template, 1, 1);
632
                tmp = template.replaceFirst("%1$d", "1");
633
                result = tmp.replaceFirst("%2$d", "1");
522 634
            }
523 635
        } else {
524 636
            //*** In case there isn't any doc Id's with the user name.
525
            result = String.format(template, 1, 1);
637
            //result = String.format(template, 1, 1);
638
            tmp = template.replaceFirst("%1$d", "1");
639
            result = tmp.replaceFirst("%2$d", "1");
526 640
        }
527 641
        return(result);
528 642
    }

Also available in: Unified diff