/* Copyright 2004-2014 Jim Voris
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.qumasoft.guitools.qwin.operation;
import com.qumasoft.guitools.qwin.QWinFrame;
import com.qumasoft.guitools.qwin.QWinUtility;
import com.qumasoft.qvcslib.ArchiveDirManagerInterface;
import com.qumasoft.qvcslib.ArchiveDirManagerProxy;
import com.qumasoft.qvcslib.requestdata.ClientRequestUnDeleteData;
import com.qumasoft.qvcslib.ClientTransactionManager;
import com.qumasoft.qvcslib.DirectoryCoordinate;
import com.qumasoft.qvcslib.DirectoryManagerFactory;
import com.qumasoft.qvcslib.DirectoryManagerInterface;
import com.qumasoft.qvcslib.MergedInfoInterface;
import com.qumasoft.qvcslib.QVCSConstants;
import com.qumasoft.qvcslib.QVCSException;
import com.qumasoft.qvcslib.RemoteProjectProperties;
import com.qumasoft.qvcslib.TransportProxyInterface;
import com.qumasoft.qvcslib.UserLocationProperties;
import com.qumasoft.qvcslib.Utility;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
/**
* Undelete an archive.
* @author Jim Voris
*/
public class OperationUnDeleteArchive extends OperationBaseClass {
/**
* Create an undelete operation.
* @param fileTable the file table.
* @param serverName the server name.
* @param projectName the project name.
* @param viewName the view name.
* @param userLocationProperties user location properties.
*/
public OperationUnDeleteArchive(JTable fileTable, String serverName, String projectName, String viewName, UserLocationProperties userLocationProperties) {
super(fileTable, serverName, projectName, viewName, userLocationProperties);
}
@Override
public void executeOperation() {
if (getFileTable() != null) {
if (getFileTable().getSelectedRowCount() == 1) {
// Get the selected files...
final List mergedInfoArray = getSelectedFiles();
// Run the update on the Swing thread.
Runnable later = new Runnable() {
@Override
public void run() {
TransportProxyInterface transportProxy = null;
int transactionID = 0;
// Ask the user if they really want to undelete the file.
int answer = JOptionPane.showConfirmDialog(QWinFrame.getQWinFrame(), "UnDelete the selected file?", "UnDelete selected files",
JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (answer == JOptionPane.YES_OPTION) {
Iterator it = mergedInfoArray.iterator();
int counter = 0;
while (it.hasNext()) {
try {
MergedInfoInterface mergedInfo = (MergedInfoInterface) it.next();
ArchiveDirManagerInterface archiveDirManager = mergedInfo.getArchiveDirManager();
ArchiveDirManagerProxy archiveDirManagerProxy = (ArchiveDirManagerProxy) archiveDirManager;
// We need to wrap this in a transaction.
if (counter == 0) {
transportProxy = archiveDirManagerProxy.getTransportProxy();
transactionID = ClientTransactionManager.getInstance().sendBeginTransaction(transportProxy);
}
lookupOrCreateUnDeleteArchiveDirProxy(mergedInfo, transportProxy, archiveDirManager);
// Request an undelete of the file.
if (mergedInfo.getLockCount() == 0) {
ClientRequestUnDeleteData clientRequest = new ClientRequestUnDeleteData();
clientRequest.setProjectName(archiveDirManager.getProjectName());
clientRequest.setViewName(archiveDirManager.getViewName());
clientRequest.setAppendedPath(archiveDirManager.getAppendedPath());
clientRequest.setShortWorkfileName(mergedInfo.getShortWorkfileName());
if (null != transportProxy) {
transportProxy.write(clientRequest);
}
// Log the success.
QWinUtility.logProblem(Level.INFO, "Sent request that archive for '" + mergedInfo.getShortWorkfileName() + "' be restored from cemetery.");
} else {
QWinUtility.logProblem(Level.WARNING, "Failed to undelete " + mergedInfo.getFullWorkfileName()
+ " . Cannot undelete a file that is locked.");
}
} catch (QVCSException e) {
QWinUtility.logProblem(Level.WARNING, "OperationUnDeleteArchive caught exception: " + e.getClass().toString() + " " + e.getLocalizedMessage());
QWinUtility.logProblem(Level.WARNING, Utility.expandStackTraceToString(e));
}
counter++;
}
ClientTransactionManager.getInstance().sendEndTransaction(transportProxy, transactionID);
}
}
};
SwingUtilities.invokeLater(later);
}
}
}
private void lookupOrCreateUnDeleteArchiveDirProxy(MergedInfoInterface mergedInfo, TransportProxyInterface transportProxy,
ArchiveDirManagerInterface archiveDirManager) throws QVCSException {
// Lookup the archive dir proxy for the directory where the undelete will restore the
// file. We do this so we're sure that it exists so that notifies from the server will
// get delivered properly.
String originalFileName = Utility.deduceOriginalFilenameForUndeleteFromCemetery(mergedInfo.getArchiveInfo());
String appendedPath = "";
if (-1 != originalFileName.lastIndexOf(QVCSConstants.QVCS_STANDARD_PATH_SEPARATOR)) {
int nameSegmentIndex = originalFileName.lastIndexOf(QVCSConstants.QVCS_STANDARD_PATH_SEPARATOR);
appendedPath = originalFileName.substring(0, nameSegmentIndex);
}
UserLocationProperties userLocationProperties = new UserLocationProperties(System.getProperty("user.dir"), transportProxy.getUsername());
String projectName = archiveDirManager.getProjectName();
String workfileBaseDirectory = userLocationProperties.getWorkfileLocation(transportProxy.getServerProperties().getServerName(), projectName, QVCSConstants.QVCS_TRUNK_VIEW);
String workfileDirectory;
if (appendedPath.length() > 0) {
workfileDirectory = workfileBaseDirectory + File.separator + appendedPath;
} else {
workfileDirectory = workfileBaseDirectory;
}
RemoteProjectProperties remoteProjectProperties = new RemoteProjectProperties(archiveDirManager.getProjectName(),
archiveDirManager.getProjectProperties().getProjectProperties());
DirectoryCoordinate directoryCoordinate = new DirectoryCoordinate(archiveDirManager.getProjectName(), QVCSConstants.QVCS_TRUNK_VIEW, appendedPath);
DirectoryManagerInterface originDirectoryManager = DirectoryManagerFactory.getInstance().getDirectoryManager(transportProxy.getServerProperties().getServerName(),
directoryCoordinate, QVCSConstants.QVCS_REMOTE_PROJECT_TYPE, remoteProjectProperties, workfileDirectory, null, false);
}
}