/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc.  Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2
 * of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * END COPYRIGHT BLOCK **/
package com.netscape.management.admserv.cmdln;


import java.util.Vector;
import java.net.URL;
import com.netscape.management.client.util.AdmTask;
import com.netscape.management.client.util.AdmTaskArg;
import com.netscape.management.client.util.Debug;


/**
 * Utility class to execute the view log entries task.
 *
 * @author  phlee
 * @version %I%, %G%
 * @seeAlso AdmTask
 */

public class ViewLogEntriesTask extends AdmTask {

    private int _status;
    private String _result;


    /**
     * Constructor to build a task url.
     *
     * @param url           the task url
     * @param userID        either username or user's DN String
     * @param userPassword  password of the users The text for the menu label
     */
    public ViewLogEntriesTask(URL url, String userID, String userPassword) {

        super(url, userID, userPassword);
        _status = 0; // Default to success.
        _result = "";
    }


    /**
      * Constructor to build a task url.
      *
      * @param admProtocol   protocol used by the target Admin Server (http/https)
      * @param admServ       host name of the target Admin Server
      * @param admPort       port number of the target Admin Server
      * @param serverID      name of the server which task will apply to
      * @param taskID        task name to be executed
      * @param args          arguments for the task
      * @param userID        either username or user's DN String
      * @param userPassword  password of the user
      */
    public ViewLogEntriesTask(String admProtocol, String admServ,
            int admPort, String serverID, String taskID, Vector args,
            String userID, String userPassword) {

        super(admProtocol, admServ, admPort, serverID, taskID, args,
                userID, userPassword);
        _status = 0; // Default to success.
        _result = "";
    }


    /**
      * Parsing specific to the view task involving the ReadLog CGI.
      *
      * @param s  the input data line to be parsed
      */
    public void parse(String s) {
        //Debug.println("TRACE ViewLogEntriesTask.parse: " + s);

        int index;
        if ((index = s.indexOf(":")) != (-1)) {
            // Check for error. Valid result also has colon to delimit time of day.
            String name = s.substring(0, index).trim();
            String value = s.substring(index + 1).trim();
            if (name.equalsIgnoreCase("status")) {
                _status = Integer.parseInt(value);
            } else if (name.equalsIgnoreCase("ErrorString")) {
                addResponseArgument(new AdmTaskArg("NMC_ErrInfo", value));
            } else {
                _result = _result + "\n" + s;
            }
        } else {
            _result = _result + "\n" + s;
        }
    }


    /**
      * Status specific to the view task involving the ReadLog CGI.
      * Need to return superclass's getStatus in case exec failed!
      *
      * @return Status of task execution.
      */
    public int getStatus() {
        if (_status != 0) {
            return _status;
        } else {
            return super.getStatus();
        }
    }


    /**
      * Result specific to the view task involving the ReadLog CGI.
      *
      * @return Result of task.
      */
    public Object getResult(String name) {
        if (name.equalsIgnoreCase("Result")) {
            return _result;
        } else {
            return null;
        }
    }
}
