-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #995 from ZakarFin/mobile
Add param handler to toggle bundles on/off based on client
- Loading branch information
Showing
3 changed files
with
98 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
control-base/src/main/java/fi/nls/oskari/control/view/modifier/param/MobileParamHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package fi.nls.oskari.control.view.modifier.param; | ||
|
||
import fi.nls.oskari.annotation.OskariViewModifier; | ||
import fi.nls.oskari.domain.map.view.Bundle; | ||
import fi.nls.oskari.domain.map.view.ViewTypes; | ||
import fi.nls.oskari.util.JSONHelper; | ||
import fi.nls.oskari.util.PropertyUtil; | ||
import fi.nls.oskari.view.modifier.ModifierException; | ||
import fi.nls.oskari.view.modifier.ModifierParams; | ||
import fi.nls.oskari.view.modifier.ParamHandler; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Configure bundles that should be removed from appsetup for mobile clients. | ||
* Configure comma-separated bundle ids in oskari-ext.properties with: | ||
* actionhandler.GetAppSetup.desktopOnly.bundles = publisher2, analyse, statsgrid, mydata, userguide, myplaces3, printout, myplacesimport, feedbackService, coordinatetransformation | ||
* actionhandler.GetAppSetup.mobileOnly.bundles = mobileuserguide | ||
*/ | ||
@OskariViewModifier("mobile") | ||
public class MobileParamHandler extends ParamHandler { | ||
|
||
private String[] desktopBundles = null; | ||
private String[] mobileBundles = null; | ||
|
||
|
||
public void init() { | ||
super.init(); | ||
desktopBundles = PropertyUtil.getCommaSeparatedList("actionhandler.GetAppSetup.desktopOnly.bundles"); | ||
mobileBundles = PropertyUtil.getCommaSeparatedList("actionhandler.GetAppSetup.mobileOnly.bundles"); | ||
} | ||
|
||
public boolean handleParam(final ModifierParams params) throws ModifierException { | ||
if (params.getParamValue() == null) { | ||
return false; | ||
} | ||
if (ViewTypes.PUBLISHED.equals(params.getView().getType())) { | ||
// don't modify embedded maps | ||
return false; | ||
} | ||
|
||
boolean isMobile = params.getParamValue().equalsIgnoreCase("true"); | ||
if (!isMobile) { | ||
// only modify when client requests mobile version | ||
return false; | ||
} | ||
List<String> bundleIds = getBundleIdsAsList(params.getStartupSequence()); | ||
// remove bundles that are not supported in mobile mode | ||
for (String bundleId : desktopBundles) { | ||
int index = bundleIds.indexOf(bundleId); | ||
if (index != -1) { | ||
// bundle is included -> drop it for mobile users | ||
// drop bundle from startup and index list | ||
params.getStartupSequence().remove(index); | ||
bundleIds.remove(index); | ||
} | ||
} | ||
// add bundles for mobile client, this can be used to inject lighter replacements for desktop bundles | ||
for (String bundleId : mobileBundles) { | ||
if (!bundleIds.contains(bundleId)) { | ||
params.getStartupSequence().put(getBundleForStartupSeq(bundleId)); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private JSONObject getBundleForStartupSeq(String bundleid) { | ||
Bundle b = new Bundle(); | ||
b.setName(bundleid); | ||
return JSONHelper.createJSONObject(b.getStartup()); | ||
} | ||
|
||
private List<String> getBundleIdsAsList(JSONArray startupSequence) { | ||
List<String> list = new ArrayList<>(startupSequence.length()); | ||
for(int i = 0; i < startupSequence.length(); ++i) { | ||
JSONObject item = startupSequence.optJSONObject(i); | ||
if (item == null) { | ||
list.add(null); | ||
} | ||
else { | ||
list.add(item.optString("bundlename")); | ||
} | ||
} | ||
return list; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters