Commit eb13bae3 authored by Adrien's avatar Adrien Committed by GitHub

feat(additional resources) (#2152)

[STUDIO-3426](https://bonitasoft.atlassian.net/browse/STUDIO-3426)
parent 16576f09
......@@ -60,5 +60,6 @@ file=File
unknownResource=This resource doesn't exist in this project.
deleteAdditionalResourceConfirmTitle=Delete confirmation
deleteAdditionalResourceConfirm=Delete the additional resource `%s` from this configuration?
barPathMissing=Some additional resources don't have name.
projectPathInvalid=Some additional resources don't exist in this project.
\ No newline at end of file
barPathInvalid=Some additional resources contain name errors.
projectPathInvalid=Some additional resources contain file errors.
barPathUnicityError=Name must be unique.
\ No newline at end of file
package org.bonitasoft.studio.document.ui.validator;
import org.bonitasoft.studio.assertions.StatusAssert;
import org.bonitasoft.studio.model.configuration.Configuration;
import org.bonitasoft.studio.model.configuration.ConfigurationFactory;
import org.bonitasoft.studio.model.configuration.Resource;
import org.eclipse.core.runtime.IStatus;
import org.junit.Test;
public class AdditionalResourceBarPathValidatorTest {
private static String BAR_PATH = "toto.txt";
AdditionalResourceBarPathValidator validator = new AdditionalResourceBarPathValidator();
@Test
public void should_return_error_for_empty_bar_path() {
Resource resource = ConfigurationFactory.eINSTANCE.createResource();
IStatus status = validator.validate(resource);
StatusAssert.assertThat(status).isError();
resource.setBarPath("");
StatusAssert.assertThat(status).isError();
}
@Test
public void should_validate_unicity() {
Configuration configuration = ConfigurationFactory.eINSTANCE.createConfiguration();
validator.setConfiguration(configuration);
Resource resource1 = ConfigurationFactory.eINSTANCE.createResource();
resource1.setBarPath(BAR_PATH);
configuration.getAdditionalResources().add(resource1);
IStatus status = validator.validate(resource1);
StatusAssert.assertThat(status).isOK();
Resource resource2 = ConfigurationFactory.eINSTANCE.createResource();
resource2.setBarPath(BAR_PATH);
configuration.getAdditionalResources().add(resource2);
status = validator.validate(resource1);
StatusAssert.assertThat(status).isError();
status = validator.validate(resource2);
StatusAssert.assertThat(status).isError();
}
}
......@@ -4,11 +4,12 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.bonitasoft.studio.assertions.StatusAssert;
import org.bonitasoft.studio.common.repository.Repository;
import org.bonitasoft.studio.common.repository.RepositoryAccessor;
import org.bonitasoft.studio.document.core.repository.DocumentFileStore;
import org.bonitasoft.studio.document.core.repository.DocumentRepositoryStore;
import org.bonitasoft.studio.model.configuration.ConfigurationFactory;
import org.bonitasoft.studio.model.configuration.Resource;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IStatus;
import org.junit.Before;
import org.junit.Test;
......@@ -22,13 +23,19 @@ public class AdditionalResourceProjectPathValidatorTest {
@Before
public void init() {
DocumentFileStore fileStore = mock(DocumentFileStore.class);
DocumentRepositoryStore repositoryStore = mock(DocumentRepositoryStore.class);
IFile knownFile = mock(IFile.class);
IFile unknownFile = mock(IFile.class);
IProject project = mock(IProject.class);
Repository repository = mock(Repository.class);
RepositoryAccessor repositoryAccessor = mock(RepositoryAccessor.class);
validator.repositoryAccessor = repositoryAccessor;
when(repositoryStore.getChild(KNOWN_RESOURCE, false)).thenReturn(fileStore);
when(repositoryAccessor.getRepositoryStore(DocumentRepositoryStore.class)).thenReturn(repositoryStore);
when(repositoryAccessor.getCurrentRepository()).thenReturn(repository);
when(repository.getProject()).thenReturn(project);
when(project.getFile(KNOWN_RESOURCE)).thenReturn(knownFile);
when(project.getFile(UNKNOWN_RESOURCE)).thenReturn(unknownFile);
when(knownFile.exists()).thenReturn(true);
when(unknownFile.exists()).thenReturn(false);
}
@Test
......@@ -43,17 +50,17 @@ public class AdditionalResourceProjectPathValidatorTest {
@Test
public void should_return_error_for_unknown_project_path() {
Resource resource = ConfigurationFactory.eINSTANCE.createResource();
IStatus status = validator.validate(resource);
resource.setProjectPath(UNKNOWN_RESOURCE);
IStatus status = validator.validate(resource);
StatusAssert.assertThat(status).isError();
}
@Test
public void should_return_ok_for_known_project_path() {
Resource resource = ConfigurationFactory.eINSTANCE.createResource();
IStatus status = validator.validate(resource);
resource.setProjectPath(KNOWN_RESOURCE);
StatusAssert.assertThat(status).isError();
IStatus status = validator.validate(resource);
StatusAssert.assertThat(status).isOK();
}
}
......@@ -84,8 +84,9 @@ public class Messages extends NLS {
public static String unknownResource;
public static String deleteAdditionalResourceConfirmTitle;
public static String deleteAdditionalResourceConfirm;
public static String barPathMissing;
public static String barPathInvalid;
public static String projectPathInvalid;
public static String barPathUnicityError;
static {
// initialize resource bundle
......
......@@ -15,8 +15,11 @@
package org.bonitasoft.studio.document.ui;
import org.bonitasoft.studio.configuration.extension.IProcessConfigurationWizardPage;
import org.bonitasoft.studio.document.SelectDocumentInBonitaStudioRepository;
import org.bonitasoft.studio.document.core.repository.DocumentFileStore;
import org.bonitasoft.studio.document.i18n.Messages;
import org.bonitasoft.studio.document.ui.editingSupport.AdditionalResourcesFileEditingSupport;
import org.bonitasoft.studio.document.ui.validator.AdditionalResourceBarPathValidator;
import org.bonitasoft.studio.document.ui.validator.AdditionalResourceProjectPathValidator;
import org.bonitasoft.studio.model.configuration.Configuration;
import org.bonitasoft.studio.model.configuration.ConfigurationFactory;
......@@ -28,7 +31,6 @@ import org.bonitasoft.studio.ui.viewer.EditingSupportBuilder;
import org.bonitasoft.studio.ui.viewer.LabelProviderBuilder;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.databinding.swt.typed.WidgetProperties;
import org.eclipse.jface.databinding.viewers.IViewerObservableValue;
......@@ -51,8 +53,6 @@ import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import com.google.common.base.Strings;
public class AdditionalResourcesConfigurationWizardPage extends WizardPage implements IProcessConfigurationWizardPage {
private DataBindingContext ctx;
......@@ -61,6 +61,7 @@ public class AdditionalResourcesConfigurationWizardPage extends WizardPage imple
private ToolItem deleteItem;
private IViewerObservableValue<Resource> singleSelectionObservable;
private IValidator<Resource> projectPathValidator;
private AdditionalResourceBarPathValidator barPathValidator;
public AdditionalResourcesConfigurationWizardPage() {
super(AdditionalResourcesConfigurationWizardPage.class.getName());
......@@ -68,6 +69,7 @@ public class AdditionalResourcesConfigurationWizardPage extends WizardPage imple
setDescription(Messages.additionalResourcesDesc);
ctx = new DataBindingContext();
projectPathValidator = new AdditionalResourceProjectPathValidator();
barPathValidator = new AdditionalResourceBarPathValidator();
}
@Override
......@@ -135,7 +137,7 @@ public class AdditionalResourcesConfigurationWizardPage extends WizardPage imple
column.setLabelProvider(new LabelProviderBuilder<Resource>()
.withTextProvider(Resource::getBarPath)
.shouldRefreshAllLabels(viewer)
.withStatusProvider(this::validateBarPath)
.withStatusProvider(barPathValidator::validate)
.createColumnLabelProvider());
column.setEditingSupport(new EditingSupportBuilder<Resource>(viewer)
.withValueProvider(Resource::getBarPath)
......@@ -143,12 +145,6 @@ public class AdditionalResourcesConfigurationWizardPage extends WizardPage imple
.create());
}
private IStatus validateBarPath(Resource resource) {
return Strings.isNullOrEmpty(resource.getBarPath())
? ValidationStatus.error(org.bonitasoft.studio.ui.i18n.Messages.required)
: ValidationStatus.ok();
}
private void createToolbar(Composite parent) {
ToolBar toolBar = new ToolBar(parent, SWT.HORIZONTAL | SWT.LEFT | SWT.NO_FOCUS | SWT.FLAT);
......@@ -171,17 +167,24 @@ public class AdditionalResourcesConfigurationWizardPage extends WizardPage imple
}
private void addItem() {
SelectDocumentInBonitaStudioRepository dialog = new SelectDocumentInBonitaStudioRepository(getShell());
dialog.open();
DocumentFileStore selectedDocument = dialog.getSelectedDocument();
if (selectedDocument != null) {
Resource resource = ConfigurationFactory.eINSTANCE.createResource();
resource.setBarPath(""); // null leads to cell editor errors..
resource.setBarPath(selectedDocument.getName());
resource.setProjectPath(selectedDocument.getResource().getProjectRelativePath().toString());
configuration.getAdditionalResources().add(resource);
viewer.refresh();
viewer.editElement(resource, 1);
}
}
@Override
public void updatePage(AbstractProcess process, Configuration configuration) {
if (process != null && configuration != null && viewer != null && !viewer.getTable().isDisposed()) {
this.configuration = configuration;
barPathValidator.setConfiguration(configuration);
viewer.setInput(configuration.getAdditionalResources());
}
}
......@@ -189,9 +192,9 @@ public class AdditionalResourcesConfigurationWizardPage extends WizardPage imple
@Override
public String isConfigurationPageValid(Configuration conf) {
boolean barPathMissing = conf.getAdditionalResources().stream()
.anyMatch(resource -> validateBarPath(resource).getSeverity() == IStatus.ERROR);
.anyMatch(resource -> barPathValidator.validate(resource).getSeverity() == IStatus.ERROR);
if (barPathMissing) {
return Messages.barPathMissing;
return Messages.barPathInvalid;
}
boolean projectPathInvalid = conf.getAdditionalResources().stream()
.anyMatch(resource -> projectPathValidator.validate(resource).getSeverity() == IStatus.ERROR);
......
......@@ -50,7 +50,7 @@ public class AdditionalResourcesFileEditingSupport extends EditingSupport {
Resource resource = (Resource) element;
if (value instanceof DocumentFileStore) {
DocumentFileStore document = (DocumentFileStore) value;
resource.setProjectPath("attachments/"+document.getName());
resource.setProjectPath(document.getResource().getProjectRelativePath().toString());
if (resource.getBarPath() == null || resource.getBarPath().isEmpty()) {
resource.setBarPath(document.getName());
}
......
/**
* Copyright (C) 2020 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.document.ui.validator;
import java.util.Objects;
import org.bonitasoft.studio.document.i18n.Messages;
import org.bonitasoft.studio.model.configuration.Configuration;
import org.bonitasoft.studio.model.configuration.Resource;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import com.google.common.base.Strings;
public class AdditionalResourceBarPathValidator implements IValidator<Resource> {
private Configuration configuration;
@Override
public IStatus validate(Resource resource) {
String barPath = resource.getBarPath();
if (Strings.isNullOrEmpty(barPath)) {
return ValidationStatus.error(org.bonitasoft.studio.ui.i18n.Messages.required);
}
if (configuration != null) {
return configuration.getAdditionalResources().stream()
.filter(rsrc -> Objects.equals(rsrc.getBarPath(), barPath))
.count() > 1
? ValidationStatus.error(Messages.barPathUnicityError)
: ValidationStatus.ok();
}
return ValidationStatus.ok();
}
public void setConfiguration(Configuration configuration) {
this.configuration = configuration;
}
}
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