/*******************************************************************************
* Copyright 2009, 2010 Innovation Gate GmbH. All Rights Reserved.
*
* This file is part of the OpenWGA server platform.
*
* OpenWGA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, a special exception is granted by the copyright holders
* of OpenWGA called "OpenWGA plugin exception". You should have received
* a copy of this exception along with OpenWGA in file COPYING.
* If not, see <http://www.openwga.com/gpl-plugin-exception>.
*
* OpenWGA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenWGA in file COPYING.
* If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package de.innovationgate.wgpublisher.webtml.utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import de.innovationgate.utils.NullPlaceHolder;
import de.innovationgate.webgate.api.WGAPIException;
import de.innovationgate.webgate.api.WGDocument;
import de.innovationgate.webgate.api.WGUserProfile;
import de.innovationgate.webgate.api.fake.WGFakeDocument;
import de.innovationgate.wga.common.CodeCompletion;
import de.innovationgate.wgpublisher.WGACore;
import de.innovationgate.wgpublisher.WGAVersion;
import de.innovationgate.wgpublisher.expressions.ExpressionEngine;
import de.innovationgate.wgpublisher.expressions.ExpressionEngineFactory;
import de.innovationgate.wgpublisher.expressions.ExpressionResult;
@CodeCompletion(methodMode=CodeCompletion.MODE_INCLUDE)
public class TMLUserProfile implements TMLObject {
public static final int RC_OK = 0;
public static final int RC_NO_DOMAIN = 1;
public static final int RC_PROFILE_EXISTS = 2;
public static final int RC_WRONG_PERSMODE = 3;
public static final int RC_NOT_CREATABLE = 4;
public static final int RC_NOT_PERSONALIZED = 5;
public static final int RC_WRONG_PASSWORD = 6;
public static final int RC_NO_PROFILE = 7;
public static final int RC_NO_SESSION = 8;
public static final int RC_METHOD_UNAVAILABLE = 9;
private WGUserProfile profile;
private boolean savedOnEnd = false;
private boolean saved = false;
private Map evaluatedClasses = Collections.synchronizedMap(new HashMap());
private WGACore core;
public TMLUserProfile(WGUserProfile profile, WGACore core) {
this.profile = profile;
this.core = core;
}
public void setpassword(String pwd) throws WGAPIException {
this.profile.setPassword(pwd);
}
private Object retrieveitem(String name) throws WGAPIException {
if (this.profile.hasItem(name)) {
return this.profile.getItemValue(name);
}
else {
// Symbolizes that the item does not exist
return new NullPlaceHolder();
}
}
@CodeCompletion
public void removeitem(String name) throws WGAPIException {
this.profile.removeItem(name);
}
@CodeCompletion
public Object item(String name) throws WGAPIException {
Object result = TMLContext.flattenList(retrieveitem(name));
if (result instanceof NullPlaceHolder) {
return profile.getDatabase().getNoItemBehaviour().getForTMLItem();
}
else {
return result;
}
}
public Object retrievemeta(String name) throws WGAPIException {
return this.profile.getMetaData(name);
}
@CodeCompletion
public Object meta(String name) throws WGAPIException {
return TMLContext.flattenList(retrievemeta(name));
}
@CodeCompletion
public boolean setitem(String name, Object value) throws WGAPIException {
return this.profile.setItemValue(name, value);
}
public boolean save(boolean force) throws WGAPIException {
setSaved(true);
if (savedOnEnd && !force) {
return true;
}
else {
boolean result = false;
try {
result = this.profile.save();
} finally {
clearclasses();
}
return result;
}
}
@CodeCompletion
public boolean save() throws WGAPIException {
return save(false);
}
public void clearclasses() {
this.evaluatedClasses.clear();
}
/**
* Returns the profile.
* @return WGUserProfile
*/
@CodeCompletion
public WGUserProfile getprofile() {
return profile;
}
/**
* @see de.innovationgate.wgpublisher.webtml.utils.TMLObject#getAPIObject()
*/
public WGDocument getapiobject() {
return this.profile;
}
@CodeCompletion
public List itemlist(String name) throws WGAPIException {
Object result = retrieveitem(name);
if (result instanceof NullPlaceHolder) {
return profile.getDatabase().getNoItemBehaviour().getForTMLItemList();
}
else {
return TMLContext.toList(result);
}
}
@CodeCompletion
public List metalist(String name) throws WGAPIException {
return TMLContext.toList(retrievemeta(name));
}
@CodeCompletion
public boolean hasitem(String name) throws WGAPIException {
return profile.hasItem(name);
}
/**
* @return Returns the savedOnEnd.
*/
public boolean isSavedOnEnd() {
return savedOnEnd;
}
/**
* @param savedOnEnd The savedOnEnd to set.
*/
public void setSavedOnEnd(boolean savedOnEnd) {
this.savedOnEnd = savedOnEnd;
}
/**
* @return Returns the saved.
*/
public boolean isSaved() {
return saved;
}
/**
* @param saved The saved to set.
*/
private void setSaved(boolean saved) {
this.saved = saved;
}
public static final void prepareNewProfile(WGUserProfile profile) throws WGAPIException {
// Set WGA Version under which this profile was created. Helps to determine
// differing storage strategies used for profiles of different versions
String version = WGAVersion.toCsConfigVersion().toString();
profile.setItemValue(WGUserProfile.ITEM_WGACREATIONVERSION, version);
}
public boolean isdummy() throws WGAPIException {
return profile.isDummy();
}
}