diff --git a/app/dv/src/main/java/io/syndesis/dv/server/endpoint/EditorService.java b/app/dv/src/main/java/io/syndesis/dv/server/endpoint/EditorService.java index 3b5ebd49117..609222d2972 100644 --- a/app/dv/src/main/java/io/syndesis/dv/server/endpoint/EditorService.java +++ b/app/dv/src/main/java/io/syndesis/dv/server/endpoint/EditorService.java @@ -250,10 +250,8 @@ ViewDefinition upsertViewEditorState(final ViewDefinition restViewDefn) { } boolean pathsSame = false; - boolean updateDv = false; // Add a new ViewDefinition if (viewDefn == null) { - updateDv = true; viewDefn = getWorkspaceManager().createViewDefiniton(restViewDefn.getDataVirtualizationName(), restViewDefn.getName()); } else { if (!restViewDefn.getName().equals(viewDefn.getName()) || !restViewDefn.getDataVirtualizationName().equals(viewDefn.getDataVirtualizationName())) { @@ -272,21 +270,26 @@ ViewDefinition upsertViewEditorState(final ViewDefinition restViewDefn) { for (String restSourcePath: restViewDefn.getSourcePaths()) { viewDefn.addSourcePath(restSourcePath); } - viewDefn.setComplete(restViewDefn.isComplete()); viewDefn.setUserDefined(restViewDefn.isUserDefined()); + boolean wasParsable = viewDefn.isParsable(); + boolean wasComplete = viewDefn.isComplete(); + + viewDefn.setComplete(restViewDefn.isComplete()); + boolean updateDv = false; + if (viewDefn.isComplete()) { if (!viewDefn.isUserDefined()) { //regenerate if needed - if (viewDefn.getDdl() == null || !pathsSame || !viewDefn.isParsable()) { + if (viewDefn.getDdl() == null || !pathsSame || !wasParsable) { String ddl = new ServiceVdbGenerator(metadataService).getODataViewDdl(viewDefn); viewDefn.setDdl(ddl); viewDefn.setParsable(true); updateDv = true; } // else we're trusting the ui - } else if (viewDefn.getDdl() != null && !Objects.equals(oldDdl, viewDefn.getDdl())) { - + } else if (viewDefn.getDdl() != null && (!Objects.equals(oldDdl, viewDefn.getDdl()) || !wasComplete)) { + viewDefn.setParsable(false); //TODO: could pro-actively validate if we're in a good state viewDefn.getSourcePaths().clear(); ValidationResult result = metadataInstance.parse(viewDefn.getDdl()); @@ -306,19 +309,17 @@ ViewDefinition upsertViewEditorState(final ViewDefinition restViewDefn) { //for now we'll redo everything updateDv = true; } - } else { - //not actually usable - perhaps come other ddl statement - if (viewDefn.isParsable()) { - updateDv = true; - } - viewDefn.setParsable(false); } } + } else { + //could be anything, so also toggle the parsable flag + viewDefn.setParsable(false); + } - if (updateDv) { - DataVirtualization dv = getWorkspaceManager().findDataVirtualization(viewDefn.getDataVirtualizationName()); - dv.touch(); - } + //if there was a change that may have affected the metadata, we need to update the preview vdb + if (updateDv || (viewDefn.isComplete() ^ wasComplete) || (viewDefn.isParsable() ^ wasParsable)) { + DataVirtualization dv = getWorkspaceManager().findDataVirtualization(viewDefn.getDataVirtualizationName()); + dv.touch(); } return viewDefn; diff --git a/app/dv/src/test/java/io/syndesis/dv/server/endpoint/EditorServiceTest.java b/app/dv/src/test/java/io/syndesis/dv/server/endpoint/EditorServiceTest.java index d0eca979a60..69aa3ae6f0b 100644 --- a/app/dv/src/test/java/io/syndesis/dv/server/endpoint/EditorServiceTest.java +++ b/app/dv/src/test/java/io/syndesis/dv/server/endpoint/EditorServiceTest.java @@ -16,13 +16,17 @@ package io.syndesis.dv.server.endpoint; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import io.syndesis.dv.metadata.internal.DefaultMetadataInstance; +import io.syndesis.dv.model.ViewDefinition; +import io.syndesis.dv.repository.RepositoryConfiguration; +import io.syndesis.dv.repository.RepositoryManagerImpl; import java.util.Arrays; - import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -35,13 +39,6 @@ import org.teiid.adminapi.impl.ModelMetaData; import org.teiid.adminapi.impl.VDBMetaData; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import io.syndesis.dv.metadata.internal.DefaultMetadataInstance; -import io.syndesis.dv.model.ViewDefinition; -import io.syndesis.dv.repository.RepositoryConfiguration; -import io.syndesis.dv.repository.RepositoryManagerImpl; - @SuppressWarnings("nls") @RunWith(SpringRunner.class) @DataJpaTest @@ -125,6 +122,22 @@ public class EditorServiceTest { for (ViewListing vl : dvService.getViewList("x")) { assertTrue(vl.isValid()); } + + //invalid + vd.setDdl("create view y as * from dummy.v"); + vd.setComplete(false); + saved = utilService.upsertViewEditorState(vd); + assertFalse(saved.isParsable()); + + //still invalid + vd.setComplete(true); + saved = utilService.upsertViewEditorState(vd); + assertFalse(saved.isParsable()); + + //still invalid - parse error after main ddl + vd.setDdl("create view y as select * from dummy.v abc 123"); + saved = utilService.upsertViewEditorState(vd); + assertFalse(saved.isParsable()); } static VDBMetaData dummyPreviewVdb(boolean hidden) {