All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.iwes.widgets.pattern.widget.init.PatternInitMultiEmpty Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2014-2018 Fraunhofer-Gesellschaft zur Förderung der angewandten Wissenschaften e.V.
 *
 * 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 de.iwes.widgets.pattern.widget.init;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.ogema.core.application.ApplicationManager;
import org.ogema.core.model.Resource;
import org.ogema.core.resourcemanager.pattern.ResourcePattern;

import de.iwes.widgets.api.extended.pattern.PatternMultiSelector;
import de.iwes.widgets.api.extended.plus.InitWidget;
import de.iwes.widgets.api.widgets.OgemaWidget;
import de.iwes.widgets.api.widgets.WidgetPage;
import de.iwes.widgets.api.widgets.sessionmanagement.OgemaHttpRequest;
import de.iwes.widgets.html.emptywidget.EmptyData;
import de.iwes.widgets.html.emptywidget.EmptyWidget;
import de.iwes.widgets.resource.widget.init.InitUtil;

/**
 * A widget that does not provide any Html, but can extract parameters from the page URL, and select corresponding patterns
 * of the specified type. This can then be used to initialize other widgets on the page.
 *
 * @param 

* @param */ public class PatternInitMultiEmpty

, R extends Resource> extends EmptyWidget implements InitWidget, PatternMultiSelector

{ private static final long serialVersionUID = 1L; private final ApplicationManager am; private List

defaultSelected = null; private final Class

type; private final boolean allowIncompletePattern; public PatternInitMultiEmpty(WidgetPage page, String id,boolean allowIncompletePattern, Class

type, ApplicationManager am) { this(page, id, false, allowIncompletePattern,type, am); } public PatternInitMultiEmpty(WidgetPage page, String id, boolean globalWidget,boolean allowIncompletePattern, Class

type, ApplicationManager am) { super(page, id, globalWidget); Objects.requireNonNull(am); this.am=am; this.type = type; this.allowIncompletePattern = allowIncompletePattern; } public PatternInitMultiEmpty(OgemaWidget parent, String id, OgemaHttpRequest req,boolean allowIncompletePattern, Class

type, ApplicationManager am) { super(parent,id, req); Objects.requireNonNull(am); this.am =am; this.type = type; this.allowIncompletePattern= allowIncompletePattern; } public class PatternInitMultiEmptyOptions extends EmptyData { private List

selectedPatterns; public PatternInitMultiEmptyOptions(PatternInitMultiEmpty empty) { super(empty); } public List

getSelectedItems() { readLock(); try { return new ArrayList<>(selectedPatterns); } finally { readUnlock(); } } public void selectItems(Collection

items) { if (items == null) items = Collections.emptyList(); writeLock(); try { this.selectedPatterns = new ArrayList<>(items); } finally { writeUnlock(); } } } @Override public PatternInitMultiEmptyOptions createNewSession() { return new PatternInitMultiEmptyOptions(this); } @SuppressWarnings("unchecked") @Override public PatternInitMultiEmptyOptions getData(OgemaHttpRequest req) { return (PatternInitMultiEmptyOptions) super.getData(req); } @Override protected void setDefaultValues(EmptyData opt) { super.setDefaultValues(opt); @SuppressWarnings("unchecked") PatternInitMultiEmptyOptions opt2= (PatternInitMultiEmptyOptions) opt; opt2.selectItems(defaultSelected); } @Override public void selectItems(Collection

items, OgemaHttpRequest req) { getData(req).selectItems(items); } @Override public void selectDefaultItems(Collection

items) { this.defaultSelected = new ArrayList<>(items); } @Override public List

getSelectedItems(OgemaHttpRequest req) { return getData(req).getSelectedItems(); } // select the pattern @Override public void init(OgemaHttpRequest req) { String[] patterns = InitUtil.getInitParameters(getPage(), req); if (patterns == null || patterns.length == 0) return; final List resources = new ArrayList<>(); for (String pt: patterns) { R aux; try { aux = am.getResourceAccess().getResource(pt); // may return null or throw an exception } catch (Exception e) { // if the type does not match am.getLogger().info("Empty pattern widget could not be initialized with the selected value {}",pt,e); aux = null; } if (aux == null || (!allowIncompletePattern && !aux.isActive()) || !aux.exists()) continue; resources.add(aux); } List

patternList = AccessController.doPrivileged(new PrivilegedAction>() { @Override public List

run() { List

result = new ArrayList<>(); for (R res: resources) { try { P pattern = type.getConstructor(Resource.class).newInstance(res); if (!allowIncompletePattern) { boolean satisfied = am.getResourcePatternAccess().isSatisfied(pattern, type); if (!satisfied) { am.getLogger().info("Found an incomplete pattern in page initialization - ignoring this: {}",pattern); continue; } } result.add(pattern); } catch (Exception e) { am.getLogger().warn("Could not initalize page with pattern type {} for resource {}",type.getName(),res,e); } } return result; } }); if (patternList == null || patternList.isEmpty()) return; am.getLogger().debug("Initializing empty pattern widget with patterns {}",patternList); getData(req).selectItems(patternList); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy