package com.perforce.bumble.connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.perforce.p4java.client.IClient;
import com.perforce.p4java.core.file.FileSpecBuilder;
import com.perforce.p4java.core.file.IFileSpec;
import com.perforce.p4java.exception.P4JavaException;
import com.perforce.p4java.server.IOptionsServer;
public abstract class DefaultService {
private final Map<String, String> map = new HashMap<>();
protected final IOptionsServer p4;
protected final IClient iclient;
protected final List<IFileSpec> spec;
public DefaultService(IOptionsServer p4) throws P4JavaException {
this.p4 = p4;
this.iclient = p4.getCurrentClient();
String ws = "//" + iclient.getName() + "/...";
this.spec = FileSpecBuilder.makeFileSpecList(ws);
buildMap();
}
protected String toLocal(IFileSpec fileSpec) throws P4JavaException {
String path = fileSpec.getDepotPathString();
for (Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (path.startsWith(key)) {
path = path.substring(key.length());
path = value + path;
return path;
}
}
return path;
}
private void buildMap() throws P4JavaException {
List<IFileSpec> list = iclient.where(spec);
for (IFileSpec l : list) {
String root = l.getDepotPathString();
root = root.replace("...", "");
String client = l.getClientPathString();
client = client.replace("...", "");
String base = "//" + iclient.getName() + "/";
client = client.substring(base.length());
map.put(root, client);
}
}
}