/**
* Copyright (c) 2011, SOCIETIES Consortium (WATERFORD INSTITUTE OF TECHNOLOGY (TSSG), HERIOT-WATT UNIVERSITY (HWU), SOLUTA.NET
* (SN), GERMAN AEROSPACE CENTRE (Deutsches Zentrum fuer Luft- und Raumfahrt e.V.) (DLR), Zavod za varnostne tehnologije
* informacijske druzbe in elektronsko poslovanje (SETCCE), INSTITUTE OF COMMUNICATION AND COMPUTER SYSTEMS (ICCS), LAKE
* COMMUNICATIONS (LAKE), INTEL PERFORMANCE LEARNING SOLUTIONS LTD (INTEL), PORTUGAL TELECOM INOVACAO, SA (PTIN), IBM Corp.,
* INSTITUT TELECOM (ITSUD), AMITEC DIACHYTI EFYIA PLIROFORIKI KAI EPIKINONIES ETERIA PERIORISMENIS EFTHINIS (AMITEC), TELECOM
* ITALIA S.p.a.(TI), TRIALOG (TRIALOG), Stiftelsen SINTEF (SINTEF), NEC EUROPE LTD (NEC))
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.societies.webapp.controller.privacy.prefs;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.societies.api.comm.xmpp.interfaces.ICommManager;
import org.societies.api.context.CtxException;
import org.societies.api.context.model.CtxAttribute;
import org.societies.api.context.model.CtxAttributeIdentifier;
import org.societies.api.context.model.CtxIdentifier;
import org.societies.api.context.model.IndividualCtxEntity;
import org.societies.api.identity.IIdentity;
import org.societies.api.identity.InvalidFormatException;
import org.societies.api.internal.context.broker.ICtxBroker;
import org.societies.api.internal.schema.privacytrust.privacyprotection.preferences.ContextPreferenceConditionBean;
import org.societies.api.internal.schema.privacytrust.privacyprotection.preferences.OperatorConstantsBean;
import org.societies.api.internal.schema.privacytrust.privacyprotection.preferences.PPNPOutcomeBean;
import org.societies.api.internal.schema.privacytrust.privacyprotection.preferences.PPNPreferenceDetailsBean;
import org.societies.api.internal.schema.privacytrust.privacyprotection.preferences.PrivacyPreferenceConditionBean;
import org.societies.api.internal.servicelifecycle.ServiceModelUtils;
import org.societies.api.schema.identity.RequestorBean;
import org.societies.api.schema.identity.RequestorCisBean;
import org.societies.api.schema.identity.RequestorServiceBean;
import org.societies.api.schema.privacytrust.privacy.model.privacypolicy.Decision;
import org.societies.api.schema.privacytrust.privacy.model.privacypolicy.Resource;
import org.societies.api.schema.servicelifecycle.model.ServiceResourceIdentifier;
import org.societies.privacytrust.privacyprotection.api.model.privacypreference.ContextPreferenceCondition;
import org.societies.privacytrust.privacyprotection.api.model.privacypreference.constants.OperatorConstants;
/**
* @author Eliza
*
*/
@RequestScoped
@ManagedBean(name="TesterBean")
public class TesterBean implements Serializable{
private final Logger logging = LoggerFactory.getLogger(getClass());
private TreeNode root;
private TreeNode selectedNode;
private List<String> ctxIds = new ArrayList<String>();
private String selectedCtxID = "";
@ManagedProperty(value="#{internalCtxBroker}")
private ICtxBroker ctxBroker;
@ManagedProperty(value="#{commMngrRef}")
private ICommManager commMgr;
private String ctxValue;
private IIdentity userId;
Hashtable<String, CtxIdentifier> ctxIDTable = new Hashtable<String, CtxIdentifier>();
private List<OperatorConstants> operators = new ArrayList<OperatorConstants>();
private OperatorConstants selectedOperator;
private PPNPreferenceDetailsBean preferenceDetails = new PPNPreferenceDetailsBean();
private int requestorType;
private String requestorService;
private String requestorCis;
private boolean preferenceDetailsCorrect = false;
public TesterBean() {
this.root = new DefaultTreeNode("Root", null);
PPNPOutcomeBean bean = new PPNPOutcomeBean();
bean.setDecision(Decision.PERMIT);
TreeNode node0 = new DefaultTreeNode(bean, root);
preferenceDetails.setRequestor(new RequestorBean());
preferenceDetails.getRequestor().setRequestorId("");
preferenceDetails.setResource(new Resource());
preferenceDetails.getResource().setDataType("");
setOperators(Arrays.asList(OperatorConstants.values()));
}
public void savePreferenceDetails(){
ServiceResourceIdentifier serviceID;
String rType = "simple";
String specific = "";
if (requestorType==0){
try {
this.logging.debug("validating cis ID:"+requestorCis);
IIdentity cisid = this.commMgr.getIdManager().fromJid(requestorCis);
((RequestorCisBean) this.preferenceDetails.getRequestor()).setCisRequestorId(cisid.getBareJid());
rType = "cis";
specific = "\nid: "+((RequestorCisBean) this.preferenceDetails.getRequestor()).getCisRequestorId();
this.logging.debug("successfully validated CIS id");
} catch (InvalidFormatException e) {
this.logging.debug("caught exception while validating cis id");
e.printStackTrace();
preferenceDetailsCorrect = false;
FacesMessage message = new FacesMessage("CIS Jid is not valid");
FacesContext.getCurrentInstance().addMessage(null, message);
return;
}
}else if (requestorType==1){
try{
this.logging.debug("validating service id: "+requestorService);
serviceID = ServiceModelUtils.generateServiceResourceIdentifierFromString(requestorService);
rType = "service";
((RequestorServiceBean) this.preferenceDetails.getRequestor()).setRequestorServiceId(serviceID);
specific = "\nid: "+((RequestorServiceBean) this.preferenceDetails.getRequestor()).getRequestorServiceId();
this.logging.debug("successfully validated service id");
}
catch (Exception e){
this.logging.debug("caught exception while generating service resource id");
e.printStackTrace();
preferenceDetailsCorrect = false;
FacesMessage message = new FacesMessage("ServiceID is not valid");
FacesContext.getCurrentInstance().addMessage(null, message);
return;
}
if (serviceID == null){
this.logging.debug("service id is null");
preferenceDetailsCorrect = false;
FacesMessage message = new FacesMessage("ServiceID is not valid");
FacesContext.getCurrentInstance().addMessage(null, message);
return;
}
}
RequestContext context = RequestContext.getCurrentInstance();
context.execute("prefDetailsDlg.hide()");
this.logging.debug("Successfully validated preferenceDetails");
this.preferenceDetailsCorrect = true;
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "PPN preference details set", "Set requestor: "+preferenceDetails.getRequestor().getRequestorId()+
"\n, type: "+rType+specific+"\nSet resource: "+preferenceDetails.getResource().getDataType());
FacesContext.getCurrentInstance().addMessage(null, message);
}
public TreeNode getSelectedNode() {
return selectedNode;
}
public void setSelectedNode(TreeNode selectedNode) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Set selected node"));
if (selectedNode==null){
this.logging.debug("setting selected node to null!");
}else{
this.logging.debug("Setting selected node: "+selectedNode.toString());
}
this.selectedNode = selectedNode;
this.printTree();
}
private void printTree(){
if (this.root==null){
this.logging.debug("root is null. tree is corrupted");
return;
}
this.logging.debug("********** <TREE **********");
String tree = "\nRoot: "+root.getData()+" "+root.getChildCount();
List<TreeNode> children = this.root.getChildren();
for (TreeNode child : children){
tree = tree.concat(getChildrenToPrint(child));
}
this.logging.debug(tree);
this.logging.debug("******** </TREE> ************");
}
private String getChildrenToPrint(TreeNode node){
String str = "\n"+node;
str = str.concat(" "+node.getChildCount());
List<TreeNode> children = node.getChildren();
for (TreeNode child : children){
str = str+child+"\n";
return str.concat(getChildrenToPrint(child));
}
return str;
}
public void displaySelectedSingle() {
if(selectedNode != null) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", selectedNode.getData().toString());
FacesContext.getCurrentInstance().addMessage(null, message);
}else{
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Not Selected", "You need to select a node to display");
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
public void createCondition(){
}
public void startAddConditionProcess(){
RequestContext.getCurrentInstance().execute("addConddlg.show();");
}
public void addCondition(){
if (selectedNode==null){
this.logging.debug("selected node is null - addCondition");
}
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", "id: "+this.selectedCtxID+", value: "+this.ctxValue);
FacesContext.getCurrentInstance().addMessage(null, message);
ContextPreferenceCondition condition = new ContextPreferenceCondition(this.ctxIDTable.get(selectedCtxID), selectedOperator, ctxValue);
if (selectedNode.getData() instanceof PPNPOutcomeBean){
TreeNode parent = selectedNode.getParent();
TreeNode node = new DefaultTreeNode(condition, parent);
selectedNode.setParent(node);
}else{
TreeNode node = new DefaultTreeNode(condition, selectedNode);
}
this.logging.debug("Added new condition to selected node:");
}
public void addOutcome(){
}
public void editNode(){
if (selectedNode !=null){
Object data = selectedNode.getData();
if (data instanceof PPNPOutcomeBean){
}
}
}
public void deleteNode() {
if (selectedNode==null){
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Node not selected", "You must select a node to delete");
FacesContext.getCurrentInstance().addMessage(null, message);
return;
}
if (selectedNode.getParent()==null){
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Deleting root node", "You can't delete the root node. If you want to cancel, this use the back button");
FacesContext.getCurrentInstance().addMessage(null, message);
return;
}
selectedNode.getChildren().clear();
selectedNode.getParent().getChildren().remove(selectedNode);
selectedNode.setParent(null);
selectedNode = null;
if (this.root.getChildCount()==0){
PPNPOutcomeBean bean = new PPNPOutcomeBean();
bean.setDecision(Decision.PERMIT);
TreeNode node0 = new DefaultTreeNode(bean, root);
}
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Deletion", "Node deleted");
FacesContext.getCurrentInstance().addMessage(null, message);
}
public List<String> getCtxIds() {
try {
this.logging.debug("Retrieving context attributes to be used as conditions");
IndividualCtxEntity individualCtxEntity = this.ctxBroker.retrieveIndividualEntity(userId).get();
Set<CtxAttribute> attributes = individualCtxEntity.getAttributes();
Iterator<CtxAttribute> iterator = attributes.iterator();
this.ctxIds.clear();
while(iterator.hasNext()){
CtxAttributeIdentifier id = iterator.next().getId();
this.ctxIds.add(id.getUri());
this.ctxIDTable.put(id.getUri(), id);
}
this.logging.debug("Found "+this.ctxIds.size()+" context attributes");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CtxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this.ctxIds;
}
public void setCtxIds(List<String> ctxIds) {
this.ctxIds = ctxIds;
}
public ICtxBroker getCtxBroker() {
return ctxBroker;
}
public void setCtxBroker(ICtxBroker ctxBroker) {
this.ctxBroker = ctxBroker;
}
public ICommManager getCommMgr() {
return commMgr;
}
public void setCommMgr(ICommManager commMgr) {
this.commMgr = commMgr;
this.userId = this.commMgr.getIdManager().getThisNetworkNode();
}
public String getCtxValue() {
return ctxValue;
}
public void setCtxValue(String ctxValue) {
this.ctxValue = ctxValue;
}
public List<OperatorConstants> getOperators() {
return operators;
}
public void setOperators(List<OperatorConstants> operators) {
this.operators = operators;
}
public OperatorConstants getSelectedOperator() {
if (selectedOperator==null){
return OperatorConstants.EQUALS;
}
return selectedOperator;
}
public void setSelectedOperator(OperatorConstants selectedOperator) {
this.selectedOperator = selectedOperator;
}
public String getSelectedCtxID() {
if (!this.ctxIds.isEmpty()){
this.selectedCtxID = ctxIds.get(0);
}
return selectedCtxID;
}
public void setSelectedCtxID(String selectedCtxID) {
this.selectedCtxID = selectedCtxID;
}
public TreeNode getRoot() {
return root;
}
public void setRoot(TreeNode root) {
this.root = root;
}
public PPNPreferenceDetailsBean getPreferenceDetails() {
return preferenceDetails;
}
public void setPreferenceDetails(PPNPreferenceDetailsBean preferenceDetails) {
this.preferenceDetails = preferenceDetails;
}
public int getRequestorType() {
return requestorType;
}
public void setRequestorType(int requestorType) {
String requestorId = this.preferenceDetails.getRequestor().getRequestorId();
switch (requestorType){
case 0:
RequestorCisBean cisBean = new RequestorCisBean();
cisBean.setRequestorId(requestorId);
this.preferenceDetails.setRequestor(cisBean);
this.logging.debug("setting requestor Type :"+requestorType);
break;
case 1:
RequestorServiceBean serviceBean = new RequestorServiceBean();
serviceBean.setRequestorId(requestorId);
this.preferenceDetails.setRequestor(serviceBean);
this.logging.debug("setting requestor Type :"+requestorType);
break;
default:
RequestorBean bean = new RequestorBean();
bean.setRequestorId(requestorId);
this.preferenceDetails.setRequestor(bean);
this.logging.debug("setting requestor Type :"+requestorType);
}
this.requestorType = requestorType;
}
public boolean isPreferenceDetailsCorrect() {
this.logging.debug("preferenceDetailsCorrect: "+preferenceDetailsCorrect);
return preferenceDetailsCorrect;
}
public void setPreferenceDetailsCorrect(boolean preferenceDetailsCorrect) {
this.preferenceDetailsCorrect = preferenceDetailsCorrect;
}
public String getRequestorService() {
return requestorService;
}
public void setRequestorService(String requestorService) {
this.requestorService = requestorService;
}
public String getRequestorCis() {
return requestorCis;
}
public void setRequestorCis(String requestorCis) {
this.requestorCis = requestorCis;
}
}