001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.so6.core.command;
034:
035: import org.libresource.so6.core.WsConnection;
036: import org.libresource.so6.core.engine.DBType;
037:
038: import java.util.Collection;
039: import java.util.Vector;
040:
041: public class Macro extends Command {
042: private static final long serialVersionUID = 3;
043: protected Command[] cmds = new Command[2];
044:
045: public Macro(Command cmd, WsConnection ws) {
046: super (cmd.getPath(), ws);
047: setTicket(cmd.getTicket());
048: }
049:
050: public String toString() {
051: return new String("Macro(" + path + "," + cmds[0] + ","
052: + cmds[1] + ")");
053: }
054:
055: public void setTicket(int ticket) {
056: if (this .getTicket() == -1) {
057: super .setTicket(ticket);
058: }
059:
060: Command cmd1 = cmds[0];
061: Command cmd2 = cmds[1];
062:
063: if (cmd1 instanceof Macro) {
064: ((Macro) cmd1).setTicket(ticket);
065: } else {
066: if (cmd1.getTicket() == -1) {
067: cmd1.setTicket(ticket);
068: }
069: }
070:
071: if (cmd2 instanceof Macro) {
072: ((Macro) cmd2).setTicket(ticket);
073: } else {
074: if (cmd2.getTicket() == -1) {
075: cmd2.setTicket(ticket);
076: }
077: }
078: }
079:
080: public Command getCommand(int index) {
081: assert (index > 0) && (index <= 2) : "index of must be 1 or 2";
082:
083: return cmds[index - 1];
084: }
085:
086: public void setCommand(Command cmd, int index) {
087: assert (index > 0) && (index <= 2) : "index of must be 1 or 2";
088: this .cmds[index - 1] = cmd;
089: }
090:
091: public Collection getCommands() {
092: Vector v = new Vector();
093: Command cmd1 = getCommand(1);
094: Command cmd2 = getCommand(2);
095:
096: if (cmd1 instanceof Macro) {
097: v.addAll(((Macro) cmd1).getCommands());
098: } else {
099: v.add(cmd1);
100: }
101:
102: if (cmd2 instanceof Macro) {
103: v.addAll(((Macro) cmd2).getCommands());
104: } else {
105: v.add(cmd2);
106: }
107:
108: return v;
109: }
110:
111: public boolean equals(Object o) {
112: if (o instanceof Macro) {
113: Macro m = (Macro) o;
114: boolean b = path.equals(m.path);
115:
116: for (int i = 0; i < cmds.length; i++) {
117: b = b && (cmds[i] != null) && (m.cmds[i] != null)
118: && cmds[i].equals(m.cmds[i]);
119: }
120:
121: return b;
122: } else {
123: return false;
124: }
125: }
126:
127: public void execute(String dir, DBType dbt) throws Exception {
128: for (int i = 0; i < cmds.length; i++) {
129: if (cmds[i] != null) {
130: //Logger.getLogger("cmd.exec").info("macro executing cmd[" + i
131: // + "]=" + cmds[i].toString());
132: cmds[i].execute(dir, dbt);
133: }
134: }
135: }
136: }
|