Package org.jboss.fresh.shell.commands

Source Code of org.jboss.fresh.shell.commands.SetFileFieldExe

package org.jboss.fresh.shell.commands;

import org.jboss.fresh.vfs.FileInfo;
import org.jboss.fresh.vfs.FileName;
import org.jboss.fresh.io.BufferObjectWriter;
import org.jboss.fresh.io.BufferWriter;
import org.jboss.fresh.io.PrintWriter2;
import org.jboss.fresh.shell.AbstractExecutable;
import org.jboss.fresh.shell.Shell;

import java.io.IOException;
import java.io.PrintWriter;

// EX OK

public class SetFileFieldExe extends AbstractExecutable {
  private static transient org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(SetFileFieldExe.class);

  /**
   dolo�imo vrednost posameznega fielda

   */

  public void process(String exepath, String[] params) throws Exception {
    log.debug("entered");
    PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));

    if (helpRequested() || (params.length < 3)) {
      usage(out);
      out.close();
      log.debug("done");
      return;
    }

    BufferObjectWriter oout = new BufferObjectWriter(getStdOut());

    Shell sh = getShell();
    FileName fname = new FileName(params[0]);
    if (!fname.isAbsolute()) {
      fname = new FileName(sh.getEnvProperty("PWD")).absolutize(fname);
    }
    log.debug("fname: " + fname);

    FileInfo inf = sh.getVFS().getFileInfo(sh.getUserCtx(), fname, true);

    log.debug("info: " + inf);
    for (int i = 1; i < params.length - 1; i++) {

      String val = params[i];
      log.debug("val: " + val);
//      if(val.equals("createDate")) {
//        inf.setCreateDate(params[++i]);
//      } else if(val.equals("lastModified")) {
//        inf.setLastModified(params[++i]);
//      } else if(val.equals("isComplete")) {
      if (val.equals("isComplete")) {
        inf.setComplete(params[++i].startsWith("t") || params[i].startsWith("T") || params[i].startsWith("1"));
      } else if (val.equals("mime")) {
        inf.setMime(params[++i]);
      } else if (val.equals("linkto")) {
        FileName fn = new FileName(params[++i]);
        if (fn.isRelative()) {
          fn = new FileName(sh.getEnvProperty("PWD")).absolutize(fn);
        }

        if (!sh.getVFS().exists(sh.getUserCtx(), fn, true)) {
          if (canThrowEx()) {
            throw new RuntimeException("The specfied linkto file does not exist:" + fn);
          } else {
            getStdOut().put(" The specified linkto file does not exist: " + fn, 10000L);
          }
          log.debug("done");
          return;
        }

        inf.setTarget(fn);
      } else {
        usage(out);
        return;
      }
    }

    sh.getVFS().updateFile(sh.getUserCtx(), inf);

    log.debug("done");
  }

  private void usage(PrintWriter out) throws IOException {
//    getStdOut().put(" Usage: filename -FIELDNAME VALUE (-FIELDNAME VALUE)\n\nWhere FIELDNAME is one of: createDate, lastModified, isComplete, mime, linkto.");
//    getStdOut().put(" Usage: filename -FIELDNAME VALUE (-FIELDNAME VALUE)\n\nWhere FIELDNAME is one of: isComplete, mime, linkto.", 10000L);

    out.println("Usage: setfilefield [-ex] [--help] file_name [field_name  field_value] ");
    out.println("    file_name : The file to change the properties in.");
    out.println("    filed_name : The field name of the file that is going to be changed. [ isComplete | mime | linkto ]");
    out.println("    field_value : The value to be set to the specified field in the specified file.");
    out.println("    --help : this help");
    out.flush();
  }

}
TOP

Related Classes of org.jboss.fresh.shell.commands.SetFileFieldExe

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.