package com.perforce.spark; import java.io.File; import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.perforce.p4java.client.IClient; import com.perforce.p4java.core.IChangelist; import com.perforce.p4java.core.file.FileSpecBuilder; import com.perforce.p4java.core.file.FileSpecOpStatus; import com.perforce.p4java.core.file.IFileSpec; import com.perforce.p4java.exception.P4JavaException; import com.perforce.p4java.impl.generic.client.ClientView; import com.perforce.p4java.impl.generic.client.ClientView.ClientViewMapping; import com.perforce.p4java.impl.generic.core.Changelist; import com.perforce.p4java.impl.mapbased.client.Client; import com.perforce.p4java.option.changelist.SubmitOptions; import com.perforce.p4java.option.client.ReconcileFilesOptions; import com.perforce.p4java.option.client.ReopenFilesOptions; import com.perforce.p4java.server.IOptionsServer; public abstract class AbstractAsset { private static Logger logger = LoggerFactory.getLogger(AbstractAsset.class); abstract Path getPath(); abstract void populateWorkspace(File root) throws Exception; public void submit(IOptionsServer p4) throws Exception { String user = p4.getUserName(); // create workspace root File root = new File("upload", user); FileUtils.deleteDirectory(root); if (!root.exists() && !root.mkdirs()) { throw new Exception("Failed to create upload directory."); } // upload files to workspace populateWorkspace(root); // setup workspace mappings String clientName = ConnectionConfig.getClientPrefix() + user; IClient iclient = p4.getClient(clientName); // create new workspace if (iclient == null) { Client implClient = new Client(p4); implClient.setName(clientName); p4.createClient(implClient); iclient = p4.getClient(clientName); } iclient.setRoot(root.getAbsolutePath()); iclient.setOwnerName(user); // create LHS 'depot' mapping Path depotPath = getPath(); String lhs = "//" + depotPath + "/..."; // create RHS 'client' mapping String rhs = "//" + clientName + "/..."; // add view mapping ClientView clientView = new ClientView(); String line = lhs + " " + rhs; ClientViewMapping entry = new ClientViewMapping(0, line); clientView.addEntry(entry); iclient.setClientView(clientView); iclient.update(); // set current client p4.setCurrentClient(iclient); // check status - find all changes to files List<IFileSpec> files = FileSpecBuilder.makeFileSpecList(rhs); ReconcileFilesOptions statusOpts = new ReconcileFilesOptions(); statusOpts.setUseWildcards(true); statusOpts.setOutsideAdd(true); List<IFileSpec> status = iclient.reconcileFiles(files, statusOpts); validateFileSpecs(status, "- no file(s) to reconcile", "instead of", "empty, assuming text", "also opened by"); for (IFileSpec s : status) { logger.info("proxy: submitting file: " + s.getDepotPathString()); } // create new pending change and add description IChangelist change = new Changelist(); change.setDescription("Asset submitted by PAM"); change = iclient.createChangelist(change); // move files from default change ReopenFilesOptions reopenOpts = new ReopenFilesOptions(); reopenOpts.setChangelistId(change.getId()); iclient.reopenFiles(files, reopenOpts); // submit pending change SubmitOptions submitOpts = new SubmitOptions(); List<IFileSpec> submitted = change.submit(submitOpts); validateFileSpecs(submitted, "Submitted as change"); // delete workspace (clean up have) p4.deleteClient(clientName, true); } /** * Look for a message in the returned FileSpec from operation. * * @param fileSpecs * @param ignore * @return * @throws ConverterException */ private void validateFileSpecs(List<IFileSpec> fileSpecs, String... ignore) throws Exception { validateFileSpecs(fileSpecs, true, ignore); } private boolean validateFileSpecs(List<IFileSpec> fileSpecs, boolean quiet, String... ignore) throws Exception { boolean success = true; boolean abort = false; ArrayList<String> ignoreList = new ArrayList<String>(); ignoreList.addAll(Arrays.asList(ignore)); for (IFileSpec fileSpec : fileSpecs) { FileSpecOpStatus status = fileSpec.getOpStatus(); if (status != FileSpecOpStatus.VALID) { String msg = fileSpec.getStatusMessage(); // superfluous p4java message boolean unknownMsg = true; for (String istring : ignoreList) { if (msg.contains(istring)) { // its a known message unknownMsg = false; break; } } // check and report unknown message if (unknownMsg) { if (!quiet) { msg = "P4JAVA: " + msg; if (status == FileSpecOpStatus.ERROR || status == FileSpecOpStatus.CLIENT_ERROR) { abort = true; } } success = false; } } } if (!quiet && abort) { String msg = "P4JAVA: Error(s)"; throw new P4JavaException(msg); } return success; } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#8 | 14883 | Paul Allen | Submit via preview and redirect to asset | ||
#7 | 14863 | Paul Allen | Change File/Dir browse to support extended options | ||
#6 | 14742 | Paul Allen | update - still issues with upload. | ||
#5 | 14741 | Paul Allen | Fix upload for multiple files. | ||
#4 | 14228 | Paul Allen |
Moved View over to the new model. Retactor connection to package. |
||
#3 | 14181 | Paul Allen | Lots of updates and refactoring. | ||
#2 | 14017 | Paul Allen | Proxy caching to Perforce and SLF4J logging. | ||
#1 | 14013 | Paul Allen | Basic proxy |