001/* 002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Julien Carsique 018 * 019 */ 020 021package org.nuxeo.connect.update.task.standalone.commands; 022 023import java.util.ArrayList; 024import java.util.List; 025import java.util.Map; 026 027import org.nuxeo.connect.update.PackageException; 028import org.nuxeo.connect.update.ValidationStatus; 029import org.nuxeo.connect.update.task.Command; 030import org.nuxeo.connect.update.task.Task; 031import org.nuxeo.connect.update.xml.XmlWriter; 032import org.w3c.dom.Element; 033 034/** 035 * Command embedding multiple commands. For internal use. 036 * 037 * @since 5.5 038 */ 039public class CompositeCommand extends AbstractCommand { 040 041 public static final String ID = "composite"; 042 043 protected final List<Command> commands; 044 045 protected CompositeCommand(String id) { 046 super(id); 047 commands = new ArrayList<Command>(); 048 } 049 050 public CompositeCommand() { 051 super(ID); 052 commands = new ArrayList<Command>(); 053 } 054 055 @Override 056 public void writeTo(XmlWriter writer) { 057 for (Command command : commands) { 058 command.writeTo(writer); 059 } 060 } 061 062 @Override 063 protected Command doRun(Task task, Map<String, String> prefs) throws PackageException { 064 CompositeCommand rollbackCommand = new CompositeCommand(); 065 for (Command command : commands) { 066 rollbackCommand.addCommand(command.run(task, prefs)); 067 } 068 return rollbackCommand; 069 } 070 071 @Override 072 protected void doValidate(Task task, ValidationStatus status) throws PackageException { 073 for (Command command : commands) { 074 command.validate(task, status); 075 } 076 } 077 078 @Override 079 public void readFrom(Element element) throws PackageException { 080 throw new UnsupportedOperationException("Composite command is for internal use only."); 081 } 082 083 public void addCommand(Command command) { 084 if (command != null) { 085 commands.add(command); 086 } 087 } 088 089 public boolean isEmpty() { 090 return commands.isEmpty(); 091 } 092 093 /** 094 * @since 9.3 095 */ 096 public CompositeCommand combine(CompositeCommand cc) { 097 commands.addAll(cc.commands); 098 return this; 099 } 100 101}