/*******************************************************************************
* Copyright (c) 2013, Perforce Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
/**
*
*/
package com.perforce.p4java.extension.operation;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.perforce.p4java.extension.command.PerforceDomain;
import com.perforce.p4java.extension.command.RequestImpl;
import com.perforce.p4java.extension.exception.ServerAccessException;
import com.perforce.p4java.extension.exception.ServerConnectionException;
import com.perforce.p4java.extension.exception.ServerErrorException;
import com.perforce.p4java.extension.path.PerforcePath;
import com.perforce.p4java.extension.path.PerforcePathFactory;
import com.perforce.p4java.extension.server.Server;
import com.perforce.p4java.extension.utility.CommandUtil;
import com.perforce.p4java.extension.utility.PathUtil;
import com.perforce.p4java.extension.utility.Validate;
/**
* Implements simplified dirs command.
*
* @since 2012.1
*/
public class DirsCommand extends Command<List<PerforcePath>> {
public static List<PerforcePath> getDirs(String path, Server server)
throws ServerConnectionException, ServerAccessException,
ServerErrorException {
Validate.notNull(path, "path must not be null to DirsCommand.getDirs");
Validate.notNull(server,
"server must not be null to DirsCommand.getDirs");
DirsCommand cmd = new DirsCommand(path);
return cmd.executeModelRequest(server);
}
public static List<PerforcePath> getDirs(final String[] paths,
final Server server) throws ServerConnectionException,
ServerAccessException, ServerErrorException {
Validate.greaterThanZero(paths.length,
"paths cannot be empty DirsCommand.getDirs");
ArrayList<String> escapedPathList = new ArrayList<String>();
for (String path : paths) {
escapedPathList.add(PathUtil.escape(path));
}
final String[] escapedPaths = escapedPathList
.toArray(new String[escapedPathList.size()]);
Map<String, Object>[] maps = server.execute(new RequestImpl() {
public String getCommand() {
return PerforceDomain.DIRS.getCommand();
}
public String[] getCommandArgs() {
return escapedPaths;
}
});
CommandUtil.checkError(maps);
List<PerforcePath> dirs = new ArrayList<PerforcePath>();
for (Map<String, Object> map : maps) {
if (map == null) {
dirs.add(null);
continue;
}
String dir = ((String) map.get("dir"));
if (dir == null) {
dirs.add(null);
} else {
dirs.add(PerforcePathFactory.getFolderPath(dir));
}
}
return dirs;
}
public static List<PerforcePath> getFullPathDirs(String path, Server server)
throws ServerConnectionException, ServerAccessException,
ServerErrorException {
Validate.notNull(path, "path must not be null to DirsCommand.getDirs");
Validate.notNull(server,
"server must not be null to DirsCommand.getDirs");
DirsCommand cmd = new DirsCommand(path) {
protected List<PerforcePath> process(Map<String, Object>[] maps) {
List<PerforcePath> dirs = new ArrayList<PerforcePath>();
for (Map<String, Object> map : maps) {
if (map == null)
continue;
String dir = ((String) map.get("dir"));
if (dir != null)
dirs.add(PerforcePathFactory.getFolderPath(dir));
}
return dirs;
}
};
return cmd.executeModelRequest(server);
}
public static List<PerforcePath> getDirsRestrictedToClient(String path,
String client, boolean viewDeleted, Server server)
throws ServerConnectionException, ServerAccessException,
ServerErrorException {
Validate.notNull(path, "path must not be null to DirsCommand.getDirs");
Validate.notNull(server,
"server must not be null to DirsCommand.getDirs");
Validate.notNull(server.getIClient(),
"server IClient must not be null to DirsCommand.getDirs");
DirsCommand cmd = new DirsCommand(path, client, viewDeleted);
return cmd.executeModelRequest(server);
}
public DirsCommand(String path) {
super(PerforceDomain.DIRS);
Validate.notNull(path, "path must not be null to DirsCommand");
init(path, null, false);
}
public DirsCommand(String path, String client) {
super(PerforceDomain.DIRS);
Validate.notNull(path, "path must not be null to DirsCommand");
init(path, client, false);
}
public DirsCommand(String path, String client, boolean viewDeleted) {
super(PerforceDomain.DIRS);
Validate.notNull(path, "path must not be null to DirsCommand");
init(path, client, viewDeleted);
}
private void init(String path, String client, boolean viewDeleted) {
if (client != null) {
arguments.add("-C");
}
if (viewDeleted) {
arguments.add("-D");
}
arguments.add(path);
}
public void addPath(String path) {
arguments.add(path);
}
public String getInputString() {
return null;
}
@Override
protected List<PerforcePath> process(Map<String, Object>[] maps) {
List<PerforcePath> dirs = new ArrayList<PerforcePath>();
for (Map<String, Object> map : maps) {
if (map == null)
continue;
// FIXME : brittle
String dir = ((String) map.get("dir"));
if (dir == null)
return null;
dirs.add(PerforcePathFactory.getFolderPath(dir));
}
return dirs;
}
}