Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Refactoring layerlisting #745

Draft
wants to merge 11 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.oskari.control.layer;

import fi.mml.map.mapwindow.util.OskariLayerWorker;
import fi.nls.oskari.annotation.OskariActionRoute;
import fi.nls.oskari.control.ActionCommonException;
import fi.nls.oskari.control.ActionException;
import fi.nls.oskari.control.ActionParameters;
import fi.nls.oskari.control.RestActionHandler;
import fi.nls.oskari.domain.map.OskariLayer;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.map.layer.DataProviderService;
import fi.nls.oskari.service.OskariComponentManager;
import fi.nls.oskari.util.ResponseHelper;
import org.oskari.control.layer.model.LayerListResponse;
import org.oskari.control.layer.model.LayerOutput;
import org.oskari.control.layer.util.ModelHelper;

import java.util.List;
import java.util.stream.Collectors;

/**
* An action route that returns layers for listing
*/
@OskariActionRoute("LayerList")
public class LayerListHandler extends RestActionHandler {

private static final Logger LOG = LogFactory.getLogger(LayerListHandler.class);

private DataProviderService dataProviderService;

public void setDataProviderService(DataProviderService service) {
this.dataProviderService = service;
}

@Override
public void init() {
// setup services if they haven't been initialized
/*
if (layerService == null) {
setLayerService(OskariComponentManager.getComponentOfType(OskariLayerService.class));
}
if (groupService == null) {
setGroupService(ServiceFactory.getOskariMapLayerGroupService());
}
if (linkService == null) {
setLinkService(new OskariLayerGroupLinkServiceMybatisImpl());
}
*/
if (dataProviderService == null) {
setDataProviderService(OskariComponentManager.getComponentOfType(DataProviderService.class));
}
}

@Override
public void handleAction(ActionParameters params) throws ActionException {
String language = params.getLocale().getLanguage();
List<OskariLayer> layers = OskariLayerWorker.getLayersForUser(params.getUser(), false);
// LayerOutput[] models =
List<LayerOutput> models = layers.stream()
.map(layer -> ModelHelper.getLayerForListing(layer, language))
// .toArray(LayerOutput[]::new);
.collect(Collectors.toList());
LayerListResponse response = new LayerListResponse(models);
response.setupProviders(dataProviderService.findAll(), language);
// TODO: groups
ResponseHelper.writeResponse(params, getString(response));
}

public static String getString(LayerListResponse output) throws ActionException {
try {
return ModelHelper.getMapper().writeValueAsString(output);
} catch (Exception e) {
throw new ActionCommonException("Error writing response", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.oskari.control.layer.model;

import java.util.HashMap;
import fi.nls.oskari.domain.map.style.VectorStyle;

import java.util.List;
Expand All @@ -8,6 +9,17 @@
public class LayerExtendedOutput extends LayerOutput {

public String coverage;

// for convenience in mapfull layers -> datasources display
// prefixed with _ to notify tmp nature
public String _dataproviderName;
// previously subtitle
public String desc;

public Boolean isQueryable;
// Mostly server-side flags but
// can include ui labels for vector feature properties etc
public Map<String, Object> attributes = new HashMap<>();
public List<VectorStyle> styles;
public Map<String, Object> hover;
public Map<String, Object> capabilities;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.oskari.control.layer.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import fi.nls.oskari.domain.map.DataProvider;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class LayerListResponse {

// TODO: groups
private List<LayerOutput> layers;
private Map<String, String> providers = new HashMap<>();

public LayerListResponse(List<LayerOutput> layers) {
this.layers = layers;
}

public void setupProviders(List<DataProvider> allProviders, String language) {
Set<Integer> referencedProviders = getProviderIds(layers);

allProviders.stream()
.forEach(provider -> {
int id = provider.getId();
if (referencedProviders.contains(id)) {
providers.put(Integer.toString(id), provider.getName(language));
}
});
}

/**
* Constructs a set of dataprovider ids that are used in the layers that will be returned to the user.
*/
@JsonIgnore
private Set<Integer> getProviderIds(List<LayerOutput> models) {
return models.stream()
.map(m -> m.dataprovider)
.collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,46 @@

import com.fasterxml.jackson.annotation.JsonInclude;

import java.util.*;

// only include non-nulls (==don't include nulls)
@JsonInclude(JsonInclude.Include.NON_NULL)
// only include lists and maps that are NOT empty
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class LayerOutput {

public int id;
// technical name
public String layer;
public String url;
public String type;
public String version;
// ui label for user
public String name;

public Double minScale;
public Double maxScale;

public Integer opacity;
public Integer refreshRate;
public Boolean realtime;
public String gfiContent;

public Integer dataprovider;
// additional URL-params/querystring for requests to service
public Map<String, Object> params = new HashMap<>();
// flags for controlling frontend behavior (wrapX on WMTS, singleTile on WMS etc)
// styles for vector features
// TODO: consider moving options and params to extended. They should not be needed before the layer is on the map
// AND having styles in options will make the listing bigger than it needs to be
public Map<String, Object> options = new HashMap<>();

public Date created;
public Date updated;

public String metadataId;
public Set<String> srs;
// do we still need "baseLayerId" or could we just put the sublayers and determine parent for them on the frontend?
public Integer baseLayerId;
// TODO: should sublayers be in extended output?
public List<LayerOutput> sublayers;
}
Loading
Loading