Commit 90ab777d authored by Romain Bioteau's avatar Romain Bioteau
Browse files

refactor import bos operation

parent 7fad6a42
......@@ -54,4 +54,12 @@ public class ImportBosArchiveOperationTest {
operationUnserTest.run(Repository.NULL_PROGRESS_MONITOR);
verify(repostioty).refresh(Repository.NULL_PROGRESS_MONITOR);
}
@Test
public void should_validate_all_imported_processes_at_the_end_of_import() throws Exception {
operationUnserTest.setCurrentRepository(repostioty);
operationUnserTest.setArchiveFile(archiveFile.getAbsolutePath());
operationUnserTest.run(Repository.NULL_PROGRESS_MONITOR);
verify(operationUnserTest).validateAllAfterImport();
}
}
/**
* Copyright (C) 2014 BonitaSoft S.A.
* BonitaSoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.common.repository.operation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.bonitasoft.studio.common.Pair;
import org.bonitasoft.studio.common.repository.model.IRepository;
import org.bonitasoft.studio.common.repository.model.IRepositoryFileStore;
import org.bonitasoft.studio.common.repository.model.IRepositoryStore;
import org.bonitasoft.studio.model.process.AbstractProcess;
import org.bonitasoft.studio.model.process.MainProcess;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
/**
* @author Romain Bioteau
*
*/
public class IResourceImporter {
final Comparator<IResource> importFolderComparator = new ImportFolderComparator();
private Set<String> resourcesToOpen;
private final List<IRepositoryFileStore> fileStoresToOpen = new ArrayList<IRepositoryFileStore>();
private final List<AbstractProcess> importedProcesses = new ArrayList<AbstractProcess>();
public void run(final IContainer rootContainer, final IRepository repository, final IProgressMonitor monitor) throws ResourceImportException {
Assert.isLegal(rootContainer != null);
Assert.isLegal(repository != null);
final List<IRepositoryStore<? extends IRepositoryFileStore>> allRepositories = repository.getAllStores();
final Map<String, IRepositoryStore<? extends IRepositoryFileStore>> repositoryStoreMap = new HashMap<String, IRepositoryStore<? extends IRepositoryFileStore>>();
for (final IRepositoryStore<? extends IRepositoryFileStore> repositoryStore : allRepositories) {
repositoryStoreMap.put(repositoryStore.getName(), repositoryStore);
}
final List<IFolder> folderSortedList = getFolders(rootContainer);
Collections.sort(folderSortedList, importFolderComparator);
for (final IFolder folder : folderSortedList) {
final Pair<IRepositoryStore<? extends IRepositoryFileStore>, IFolder> pair = findRepository(
repositoryStoreMap, folder);
if (pair != null) {
importRepositoryStore(pair, monitor);
}
}
}
public List<AbstractProcess> getImportedProcesses() {
return importedProcesses;
}
public List<IRepositoryFileStore> getFileStoresToOpen() {
return fileStoresToOpen;
}
private Pair<IRepositoryStore<? extends IRepositoryFileStore>, IFolder> findRepository(
final Map<String, IRepositoryStore<? extends IRepositoryFileStore>> repositoryStoreMap,
final IFolder folder) throws ResourceImportException {
final String path = folder.getProjectRelativePath().removeFirstSegments(1).toOSString();
final IRepositoryStore<? extends IRepositoryFileStore> store = repositoryStoreMap.get(path);
if (store != null) {
return new Pair<IRepositoryStore<? extends IRepositoryFileStore>, IFolder>(store, folder);
} else {
for (final IFolder subFolder : getFolders(folder)) {
final Pair<IRepositoryStore<? extends IRepositoryFileStore>, IFolder> pair = findRepository(repositoryStoreMap, subFolder);
if (pair != null) {
return pair;
}
}
}
return null;
}
protected List<IFolder> getFolders(final IContainer container) throws ResourceImportException {
IResource[] folders;
try {
folders = container.members(IContainer.FOLDER);
} catch (final CoreException e) {
throw new ResourceImportException("Failed to retrieve folders of resource " + container.getName(), e);
}
final List<IFolder> folderList = new ArrayList<IFolder>();
if (folders != null) {
for (final IResource res : folders) {
if (res instanceof IFolder) {
folderList.add((IFolder) res);
}
}
}
return folderList;
}
protected void importRepositoryStore(final Pair<IRepositoryStore<? extends IRepositoryFileStore>, IFolder> pair, final IProgressMonitor monitor)
throws ResourceImportException {
final IFolder storeFolder = pair.getSecond();
final IRepositoryStore<? extends IRepositoryFileStore> store = pair.getFirst();
IResource[] members = null;
try {
members = storeFolder.members();
} catch (final CoreException e) {
throw new ResourceImportException("Failed to retreive members of resource :" + storeFolder.getName(), e);
}
for (final IResource child : members) {
final String filename = child.getName();
final boolean openAfterImport = resourcesToOpen != null && resourcesToOpen.contains(filename)
|| resourcesToOpen == null;
final IRepositoryFileStore fileStore = store.importIResource(filename, child);
if (filename.endsWith(".proc")) {
importedProcesses.add((MainProcess) fileStore.getContent());
}
if (fileStore != null && openAfterImport) {
fileStoresToOpen.add(fileStore);
}
}
}
public void setResourcesToOpen(final Set<String> resourcesToOpen) {
this.resourcesToOpen = resourcesToOpen;
}
}
......@@ -21,10 +21,8 @@ import static org.bonitasoft.studio.common.Messages.bosProductName;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
......@@ -32,7 +30,6 @@ import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.bonitasoft.studio.common.Pair;
import org.bonitasoft.studio.common.ProcessesValidationAction;
import org.bonitasoft.studio.common.ProductVersion;
import org.bonitasoft.studio.common.jface.FileActionDialog;
......@@ -47,7 +44,6 @@ import org.bonitasoft.studio.common.repository.model.IRepository;
import org.bonitasoft.studio.common.repository.model.IRepositoryFileStore;
import org.bonitasoft.studio.common.repository.model.IRepositoryStore;
import org.bonitasoft.studio.model.process.AbstractProcess;
import org.bonitasoft.studio.model.process.MainProcess;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
......@@ -72,97 +68,90 @@ public class ImportBosArchiveOperation {
private static final String TMP_IMPORT_PROJECT = "tmpImport";
private String archiveFile;
private Set<String> resourceToOpen;
private final List<IRepositoryFileStore> fileStoresToOpen = new ArrayList<IRepositoryFileStore>();
private IRepository currentRepository;
private final List<AbstractProcess> importedProcesses = new ArrayList<AbstractProcess>();
private BosArchiveImportStatus importStatus;
private final IResourceImporter iResourceImporter;
public ImportBosArchiveOperation() {
iResourceImporter = new IResourceImporter();
}
public IStatus run(final IProgressMonitor monitor) {
Assert.isNotNull(archiveFile);
Assert.isNotNull(currentRepository);
final File archive = new File(archiveFile);
Assert.isTrue(archive.exists());
fileStoresToOpen.clear();
try {
currentRepository.disableBuild();
final IContainer container = createTempProject(archive, monitor);
final Map<String, IRepositoryStore<? extends IRepositoryFileStore>> repositoryMap = new HashMap<String, IRepositoryStore<? extends IRepositoryFileStore>>();
currentRepository.notifyFileStoreEvent(new FileStoreChangeEvent(EventType.PRE_IMPORT, null));
final List<IRepositoryStore<? extends IRepositoryFileStore>> allRepositories = currentRepository.getAllStores();
currentRepository.disableBuild();
IContainer container;
try {
container = createTempProject(archive, monitor);
} catch (final CoreException e1) {
restoreBuildState();
return new Status(IStatus.ERROR, CommonRepositoryPlugin.PLUGIN_ID, "Failed to create temporary project", e1);
}
FileActionDialog.activateYesNoToAll();
currentRepository.notifyFileStoreEvent(new FileStoreChangeEvent(EventType.PRE_IMPORT, null));
final List<IRepositoryStore<? extends IRepositoryFileStore>> allRepositories = currentRepository.getAllStores();
for (final IRepositoryStore<? extends IRepositoryFileStore> repository : allRepositories) {
repositoryMap.put(repository.getName(), repository);
}
FileActionDialog.activateYesNoToAll();
final Map<String, IRepositoryStore<? extends IRepositoryFileStore>> repositoryMap = new HashMap<String, IRepositoryStore<? extends IRepositoryFileStore>>();
for (final IRepositoryStore<? extends IRepositoryFileStore> repository : allRepositories) {
repositoryMap.put(repository.getName(), repository);
}
final IContainer rootContainer = getRootContainer(container, repositoryMap);
IContainer rootContainer = null;;
try {
rootContainer = getRootContainer(container, repositoryMap);
if (rootContainer == null) {
restoreBuildState();
return new Status(IStatus.ERROR, CommonRepositoryPlugin.PLUGIN_ID, Messages.bind(Messages.invalidArchive, new Object[] { bosProductName }));
}
} catch (final CoreException e1) {
restoreBuildState();
return new Status(IStatus.ERROR, CommonRepositoryPlugin.PLUGIN_ID, "Failed to retrieve root container", e1);
}
updateResourcesToOpenList(rootContainer);
FileActionDialog.activateYesNoToAll();
final IResource[] folders = rootContainer.members(IContainer.FOLDER);
if (folders != null) {
final List<IResource> folderSortedList = new ArrayList<IResource>(Arrays.asList(folders));
final Comparator<IResource> importFolderComparator = new ImportFolderComparator();
Collections.sort(folderSortedList, importFolderComparator);
for (final IResource folder : folderSortedList) {
try {
if (folder instanceof IFolder) {
Pair<IRepositoryStore<? extends IRepositoryFileStore>, IFolder> pair = findRepository(
repositoryMap, (IFolder) folder);
if (pair == null) {
for (final IResource subFolder : ((IContainer) folder).members(IContainer.FOLDER)) {
if (subFolder instanceof IFolder) {
pair = findRepository(repositoryMap, (IFolder) subFolder);
if (pair != null) {
importRepositoryStore(pair, monitor);
}
}
}
} else if (pair != null) {
importRepositoryStore(pair, monitor);
}
}
} catch (final Exception e) {
BonitaStudioLog.error(e);
}
}
}
FileActionDialog.deactivateYesNoToAll();
currentRepository.enableBuild();
currentRepository.refresh(Repository.NULL_PROGRESS_MONITOR);
currentRepository.notifyFileStoreEvent(new FileStoreChangeEvent(EventType.POST_IMPORT, null));
validateAllAfterImport();
} catch (final Exception e) {
BonitaStudioLog.error(e);
checkArchiveCompatibility(rootContainer);
FileActionDialog.activateYesNoToAll();
iResourceImporter.setResourcesToOpen(getResourcesToOpen(rootContainer));
try {
iResourceImporter.run(rootContainer, currentRepository, monitor);
} catch (final ResourceImportException e) {
return new Status(IStatus.ERROR, CommonRepositoryPlugin.PLUGIN_ID, "Failed to import resources in " + currentRepository.getName(), e);
} finally {
if(!currentRepository.isBuildEnable()){
currentRepository.enableBuild();
}
cleanTmpProject();
restoreBuildState();
}
FileActionDialog.deactivateYesNoToAll();
currentRepository.refresh(Repository.NULL_PROGRESS_MONITOR);
currentRepository.notifyFileStoreEvent(new FileStoreChangeEvent(EventType.POST_IMPORT, null));
validateAllAfterImport();
return Status.OK_STATUS;
}
protected void restoreBuildState() {
if (!currentRepository.isBuildEnable()) {
currentRepository.enableBuild();
}
cleanTmpProject();
}
public IStatus getValidationsStatus() {
return importStatus;
}
private void validateAllAfterImport() {
protected void validateAllAfterImport() {
final ImportBosArchiveStatusBuilder statusBuilder = new ImportBosArchiveStatusBuilder();
for (final AbstractProcess process : importedProcesses) {
for (final AbstractProcess process : iResourceImporter.getImportedProcesses()) {
final ProcessesValidationAction validationAction = new ProcessesValidationAction(Collections.singletonList(process));
Display.getDefault().syncExec(new Runnable() {
......@@ -184,8 +173,7 @@ public class ImportBosArchiveOperation {
protected IContainer getRootContainer(
IContainer container,
final Map<String, IRepositoryStore<? extends IRepositoryFileStore>> repositoryMap)
throws CoreException {
final Map<String, IRepositoryStore<? extends IRepositoryFileStore>> repositoryMap) throws CoreException {
boolean isValid = false;
while (container != null && !isValid) {
IResource lastVisited = null;
......@@ -212,35 +200,28 @@ public class ImportBosArchiveOperation {
}
public void openFilesToOpen() {
for (final IRepositoryFileStore f : fileStoresToOpen) {
for (final IRepositoryFileStore f : getFileStoresToOpen()) {
f.open();
}
}
public List<IRepositoryFileStore> getFileStoresToOpen() {
return fileStoresToOpen;
return iResourceImporter.getFileStoresToOpen();
}
protected void importRepositoryStore(final Pair<IRepositoryStore<? extends IRepositoryFileStore>, IFolder> pair, final IProgressMonitor monitor)
throws CoreException {
final IFolder storeFolder = pair.getSecond();
final IRepositoryStore<? extends IRepositoryFileStore> repository = pair.getFirst();
for (final IResource child : storeFolder.members()) {
final String filename = child.getName();
final boolean openAfterImport = resourceToOpen != null && resourceToOpen.contains(filename)
|| resourceToOpen == null;
final IRepositoryFileStore fileStore = repository.importIResource(filename, child);
if (filename.endsWith(".proc")) {
importedProcesses.add((MainProcess) fileStore.getContent());
}
if (fileStore != null && openAfterImport) {
fileStoresToOpen.add(fileStore);
}
protected Set<String> getResourcesToOpen(final IContainer container) {
final Properties manifestProperties = getManifestInfo(container);
final String toOpen = manifestProperties.getProperty(ExportBosArchiveOperation.TO_OPEN);
if (toOpen != null) {
final String[] array = toOpen.split(",");
return new HashSet<String>(Arrays.asList(array));
}
return Collections.emptySet();
}
private void updateResourcesToOpenList(final IContainer container) {
protected void checkArchiveCompatibility(final IContainer container) {
final Properties manifestProperties = getManifestInfo(container);
if (manifestProperties != null) {
final String version = manifestProperties.getProperty(ExportBosArchiveOperation.VERSION);
......@@ -254,14 +235,8 @@ public class ImportBosArchiveOperation {
Messages.bind(Messages.incompatibleProductVersion, ProductVersion.CURRENT_VERSION, version));
}
});
throw new RuntimeException(Messages.bind(Messages.incompatibleProductVersion, ProductVersion.CURRENT_VERSION, version));
}
final String toOpen = manifestProperties.getProperty(ExportBosArchiveOperation.TO_OPEN);
if (toOpen != null) {
final String[] array = toOpen.split(",");
resourceToOpen = new HashSet<String>(Arrays.asList(array));
}
}
}
......@@ -337,16 +312,6 @@ public class ImportBosArchiveOperation {
return null;
}
private Pair<IRepositoryStore<? extends IRepositoryFileStore>, IFolder> findRepository(
final Map<String, IRepositoryStore<? extends IRepositoryFileStore>> map, final IFolder folder) {
final String path = folder.getProjectRelativePath().removeFirstSegments(1).toOSString();
final IRepositoryStore<? extends IRepositoryFileStore> store = map.get(path);
if (store != null) {
return new Pair<IRepositoryStore<? extends IRepositoryFileStore>, IFolder>(store, folder);
}
return null;
}
public void setArchiveFile(final String archiveFile) {
this.archiveFile = archiveFile;
......
/**
* Copyright (C) 2014 BonitaSoft S.A.
* BonitaSoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.common.repository.operation;
/**
* @author Romain Bioteau
*
*/
public class ResourceImportException extends Exception {
private static final long serialVersionUID = 6655462865137461353L;
public ResourceImportException(final String message, final Throwable exception) {
super(message, exception);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment