Skip to content

Commit

Permalink
AF-2700: Create Heatmap internal component (#1080)
Browse files Browse the repository at this point in the history
* AF-2700: Create Heatmap internal component

* Only used external components are exported

* Review changes

* Adding test for listAllComponents

* Removing static functions and using a class for component Api

* improving process selector

* Making process selector expanded again

* increasing version and removing sample logo
  • Loading branch information
jesuino authored Dec 11, 2020
1 parent b7698fd commit 774d14e
Show file tree
Hide file tree
Showing 89 changed files with 6,018 additions and 1,161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,17 @@ public class ExternalComponentServlet extends HttpServlet {
ComponentLoader loader;

String cacheControlHeaderValue = "no-cache";
private MimetypesFileTypeMap mimetypesFileTypeMap;
private MimetypesFileTypeMap mimeTypes;

@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
mimetypesFileTypeMap = new MimetypesFileTypeMap();
mimeTypes = new MimetypesFileTypeMap();
String cacheControl = config.getInitParameter(CACHE_CONTROL_PARAM);
if (cacheControl != null) {
cacheControlHeaderValue = cacheControl;
}
addAdditionalMimeTypes();
}

@Override
Expand Down Expand Up @@ -95,7 +96,7 @@ private void handle(HttpServletRequest req, HttpServletResponse resp) throws IOE

try (InputStream assetStream = assetProvider.openAsset(assetPath)) {
int size = IOUtils.copy(assetStream, resp.getOutputStream());
String mimeType = mimetypesFileTypeMap.getContentType(pathInfo);
String mimeType = mimeTypes.getContentType(pathInfo);
resp.setContentType(mimeType);
resp.setContentLength(size);
resp.setHeader(CACHE_CONTROL_PARAM, cacheControlHeaderValue);
Expand All @@ -119,4 +120,9 @@ private void errorResponse(HttpServletResponse resp) {
}
}

private void addAdditionalMimeTypes() {
mimeTypes.addMimeTypes("text/javascript js");
mimeTypes.addMimeTypes("text/css css");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,17 @@ public Optional<ExternalComponent> byId(String componentId) {
.filter(c -> componentId.equals(c.getId()))
.findFirst();
}

@Override
public List<ExternalComponent> listAllComponents() {
List<ExternalComponent> allComponents = loader.loadExternal();
List<ExternalComponent> provided = loader.loadProvided();

allComponents.forEach(c -> c.setProvided(false));
provided.forEach(c -> c.setProvided(true));
allComponents.addAll(provided);

return allComponents;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void testLoadInternalComponents() throws IOException {
externalComponentLoaderImpl.init();
List<ExternalComponent> internalComponents = externalComponentLoaderImpl.loadProvided();

assertEquals(1, internalComponents.size());
assertEquals(3, internalComponents.size());

ExternalComponent component = internalComponents.get(0);
assertEquals("logo-provided", component.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.dashbuilder.external.impl;

import java.util.Collections;
import java.util.List;

import org.dashbuilder.external.model.ExternalComponent;
import org.dashbuilder.external.service.ComponentLoader;
Expand All @@ -31,6 +32,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ComponentServiceImplTest {
Expand Down Expand Up @@ -62,10 +64,26 @@ public void testByIdProvidedPriority() {
ExternalComponent c1_provided = new ExternalComponent(C1_ID, c1ProvidedName, "c1 icon", false, Collections.emptyList());
ExternalComponent c1_external = new ExternalComponent(C1_ID, "c1 external", "c1 icon", false, Collections.emptyList());

Mockito.when(loader.loadProvided()).thenReturn(asList(c1_provided));
Mockito.when(loader.loadExternal()).thenReturn(asList(c1_external));
when(loader.loadProvided()).thenReturn(asList(c1_provided));
when(loader.loadExternal()).thenReturn(asList(c1_external));

assertEquals(c1ProvidedName, externalComponentServiceImpl.byId(C1_ID).get().getName());
}

public void testListAllComponents() {
String providedId = "c1";
String externalId = "c2";
ExternalComponent c1_provided = new ExternalComponent(providedId, "name", "icon", false, Collections.emptyList());
ExternalComponent c1_external = new ExternalComponent(externalId, "name", "icon", false, Collections.emptyList());

when(loader.loadProvided()).thenReturn(asList(c1_provided));
when(loader.loadExternal()).thenReturn(asList(c1_external));

List<ExternalComponent> comps = externalComponentServiceImpl.listAllComponents();
ExternalComponent cp = comps.stream().filter(c -> providedId.equals(c.getId())).findAny().get();
ExternalComponent ce = comps.stream().filter(c -> externalId.equals(c.getId())).findAny().get();
assertTrue(cp.isProvided());
assertFalse(ce.isProvided());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
import static org.dashbuilder.dataset.DataSetDefRegistryCDI.DATASET_EXT;
import static org.uberfire.ext.plugin.model.Plugin.FILE_EXT;

@ApplicationScoped
@Service
@ApplicationScoped
public class DataTransferServicesImpl implements DataTransferServices {

public static final String VERSION = "1.0.0";
Expand All @@ -86,6 +86,7 @@ public class DataTransferServicesImpl implements DataTransferServices {
private NavTreeStorage navTreeStorage;
private byte[] buffer = new byte[1024];
private ComponentLoader externalComponentLoader;
private LayoutComponentHelper layoutComponentsHelper;

private String dashbuilderLocation;
private String exportDir;
Expand All @@ -108,7 +109,8 @@ public DataTransferServicesImpl(
final Event<PluginAdded> pluginAddedEvent,
final Event<NavTreeChangedEvent> navTreeChangedEvent,
final NavTreeStorage navTreeStorage,
final ComponentLoader externalComponentLoader) {
final ComponentLoader externalComponentLoader,
final LayoutComponentHelper layoutComponentsHelper) {

this.ioService = ioService;
this.datasetsFS = datasetsFS;
Expand All @@ -122,6 +124,7 @@ public DataTransferServicesImpl(
this.navTreeChangedEvent = navTreeChangedEvent;
this.navTreeStorage = navTreeStorage;
this.externalComponentLoader = externalComponentLoader;
this.layoutComponentsHelper = layoutComponentsHelper;
}

@PostConstruct
Expand Down Expand Up @@ -175,13 +178,18 @@ public String doExport(DataTransferExportModel exportModel) throws java.io.IOExc
.append("://")
.append(componentsPath)
.toString());
externalComponentLoader.loadExternal().forEach(c -> {
Path componentPath = componentsBasePath.resolve(c.getId());
zipComponentFiles(componentsBasePath,
componentPath,
zos,
p -> true);
});
Predicate<String> pagesComponentsFilter = page -> exportModel.isExportAll() || exportModel.getPages().contains(page);
layoutComponentsHelper.findComponentsInTemplates(pagesComponentsFilter)
.stream()
.map(c -> componentsBasePath.resolve(c))
.filter(Files::exists)
.forEach(c -> {
Path componentPath = componentsBasePath.resolve(c);
zipComponentFiles(componentsBasePath,
componentPath,
zos,
p -> true);
});
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2019 Red Hat, Inc. and/or its affiliates.
*
* 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 org.dashbuilder.transfer;

import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.dashbuilder.external.model.ExternalComponent;
import org.uberfire.ext.layout.editor.api.PerspectiveServices;
import org.uberfire.ext.layout.editor.api.editor.LayoutComponent;
import org.uberfire.ext.layout.editor.api.editor.LayoutRow;
import org.uberfire.ext.layout.editor.api.editor.LayoutTemplate;

@ApplicationScoped
public class LayoutComponentHelper {

@Inject
private PerspectiveServices perspectiveServices;

public List<String> findComponentsInTemplates(Predicate<String> pageFilter) {
return perspectiveServices.listLayoutTemplates()
.stream()
.filter(lt -> pageFilter.test(lt.getName()))
.map(LayoutTemplate::getRows)
.flatMap(this::allComponentsStream)
.map(lt -> lt.getProperties().get(ExternalComponent.COMPONENT_ID_KEY))
.filter(Objects::nonNull)
.collect(Collectors.toList());
}

private Stream<LayoutComponent> allComponentsStream(List<LayoutRow> row) {
return row.stream()
.flatMap(r -> r.getLayoutColumns().stream())
.flatMap(cl -> Stream.concat(cl.getLayoutComponents().stream(),
allComponentsStream(cl.getRows())));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public class DataTransferServicesTest {
DataSetDefJSONMarshaller dataSetDefJSONMarshaller;
@Mock
ComponentLoader externalComponentLoader;
@Mock
LayoutComponentHelper layoutComponentsHelper;

Path componentsDir;

Expand Down Expand Up @@ -124,7 +126,8 @@ public void setup() {
pluginAddedEvent,
navTreeChangedEvent,
navTreeStorage,
externalComponentLoader);
externalComponentLoader,
layoutComponentsHelper);
}

@After
Expand Down Expand Up @@ -301,7 +304,7 @@ public void testDoExportWithoutNavigation() throws Exception {

@Test
public void testDoExportWithComponents() throws Exception {
when(externalComponentLoader.loadExternal()).thenReturn(asList(component("c1")));
when(layoutComponentsHelper.findComponentsInTemplates((any()))).thenReturn(asList("c1"));

createFile(perspectivesFS, "page1/perspective_layout", "");
createFile(perspectivesFS, "page1/perspective_layout.plugin", "");
Expand All @@ -314,6 +317,13 @@ public void testDoExportWithComponents() throws Exception {
// lost file in component Dir that should be ignored
createComponentFile("lost", "lostfile", "ignore-me-import");

// Other component that is not used so it should not be exported
createComponentFile("c2", "manifest.json", "manifest");
createComponentFile("c2", "index.html", "html");
createComponentFile("c2", "css/style.css", "style");
createComponentFile("c2", "js/index.js", "js");


dataTransferServices.doExport(DataTransferExportModel.exportAll());

ZipInputStream zis = getZipInputStream();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2020 Red Hat, Inc. and/or its affiliates.
*
* 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 org.dashbuilder.transfer;

import java.util.Arrays;
import java.util.List;

import org.dashbuilder.external.model.ExternalComponent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.uberfire.ext.layout.editor.api.PerspectiveServices;
import org.uberfire.ext.layout.editor.api.editor.LayoutColumn;
import org.uberfire.ext.layout.editor.api.editor.LayoutComponent;
import org.uberfire.ext.layout.editor.api.editor.LayoutRow;
import org.uberfire.ext.layout.editor.api.editor.LayoutTemplate;

import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class LayoutComponentHelperTest {

@Mock
PerspectiveServices perspectiveServices;

@InjectMocks
LayoutComponentHelper layoutComponentsHelper;

@Test
public void testComponentId() {
String c1 = "c1";
String c2 = "c2";
LayoutTemplate lt = createLayoutTemplate("lt", c1, c2);
when(perspectiveServices.listLayoutTemplates()).thenReturn(singletonList(lt));

List<String> components = layoutComponentsHelper.findComponentsInTemplates(p -> true);

assertEquals(2, components.size());
assertEquals(components, Arrays.asList(c1, c2));
}

public void testNoComponentId() {
LayoutTemplate lt = createLayoutTemplate("lt");
when(perspectiveServices.listLayoutTemplates()).thenReturn(singletonList(lt));

List<String> components = layoutComponentsHelper.findComponentsInTemplates(p -> true);

assertTrue(components.isEmpty());
}

public void testPageFilter() {
String c1 = "c1";
LayoutTemplate lt = createLayoutTemplate("lt", c1);
when(perspectiveServices.listLayoutTemplates()).thenReturn(singletonList(lt));

List<String> components = layoutComponentsHelper.findComponentsInTemplates(p -> false);

assertTrue(components.isEmpty());
}

private LayoutTemplate createLayoutTemplate(String name, String... componentIds) {
LayoutTemplate lt = new LayoutTemplate(name);
LayoutRow lr = new LayoutRow();
LayoutColumn lc = new LayoutColumn("");

lr.add(lc);
lt.addRow(lr);
for (String componentId : componentIds) {
LayoutComponent lComp = new LayoutComponent();
lComp.addProperty(ExternalComponent.COMPONENT_ID_KEY, componentId);
lc.add(lComp);
}
return lt;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import javax.enterprise.context.Dependent;
import javax.inject.Inject;

import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.Event;
import org.dashbuilder.client.cms.resources.i18n.ContentManagerConstants;
import org.gwtbootstrap3.client.ui.Modal;
Expand Down Expand Up @@ -187,6 +188,16 @@ public void okClick(final Event event) {
buttonPressed = ButtonPressed.OK;
presenter.onOK();
}

@SinkNative(Event.ONMOUSEDOWN)
@EventHandler("nameInput")
public void nameInputEnter(final Event event) {
if (event.getKeyCode() == KeyCodes.KEY_ENTER) {
buttonPressed = ButtonPressed.OK;
presenter.onOK();
}
}


@SinkNative(Event.ONCLICK)
@EventHandler("cancelButton")
Expand Down
Loading

0 comments on commit 774d14e

Please sign in to comment.