/*******************************************************************************
* 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.webgate.api.jdbc;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.Hibernate;
import de.bannkreis.groq.Groq;
import de.bannkreis.groq.GroqFilterSet;
import de.innovationgate.utils.UIDGenerator;
import de.innovationgate.utils.WGUtils;
import de.innovationgate.webgate.api.WGAPIException;
import de.innovationgate.webgate.api.WGIllegalArgumentException;
import de.innovationgate.webgate.api.WGPortlet;
import de.innovationgate.webgate.api.WGPortletDefaultBean;
import de.innovationgate.webgate.api.WGPortletRegistry;
import de.innovationgate.webgate.api.WGUserProfile;
import de.innovationgate.wga.common.ImmutableObject;
public class PortletRegistryImplV5 extends WGPortletRegistry implements ImmutableObject {
private WGDatabaseImpl _parent;
private WGDocumentImpl _docImpl;
private UserProfile _profile;
protected PortletRegistryImplV5(WGDatabaseImpl parent, WGDocumentImpl docImpl) {
_parent = parent;
_docImpl = docImpl;
_profile = (UserProfile) docImpl.getNativeObject();
}
@Override
public List<WGPortlet> getChildPortlets(WGPortlet portlet) throws WGAPIException {
List<UserProfilePortlet> childWGPortlets = new ArrayList<UserProfilePortlet>();
return Groq.selectFrom(_profile.getPortlets().values()).wherePropertyEquals("parentportlet", portlet).list();
}
@Override
public List<String> getItemNames(WGPortlet portlet) throws WGAPIException {
return new ArrayList<String>(((UserProfilePortlet) portlet).getItems().keySet());
}
@Override
public Object getItemValue(WGPortlet portlet, String name) throws WGAPIException {
String lcName = name.toLowerCase().trim();
UserProfileItem item = ((UserProfilePortlet) portlet).getItems().get(lcName);
if (item != null) {
return WGDocumentImpl.readItemValue(item);
}
// Workaround for wrongly stored items due to bug #00000316
item = ((UserProfilePortlet) portlet).getItems().get(name);
if (item != null) {
return WGDocumentImpl.readItemValue(item);
}
return null;
}
@Override
public WGPortlet getPortlet(String appDb, String key) throws WGAPIException {
if (key.startsWith(appDb)) {
return getRootPortlet(appDb);
}
return (WGPortlet) _profile.getPortlets().get(key);
}
@Override
public List<String> getPortletIds(String appDb) throws WGAPIException {
List<String> keys = new ArrayList<String>();
Iterator<UserProfilePortlet> portlets = Groq.selectFrom(_profile.getPortlets().values()).wherePropertyEquals("appdb",appDb);
while (portlets.hasNext()) {
UserProfilePortlet userProfilePortlet = portlets.next();
keys.add(userProfilePortlet.getKey());
}
return keys;
}
@Override
public WGPortlet getRootPortlet(String dbKey) throws WGAPIException {
GroqFilterSet<UserProfilePortlet> rootPortlets = Groq.selectFrom(_profile.getPortlets().values())
.wherePropertyEquals("parentportlet", null)
.wherePropertyEquals("appdb", dbKey);
if (rootPortlets.hasNext()) {
return rootPortlets.next();
}
// Root portlet not yet registered. We create one
else {
UserProfilePortlet rootPortlet = (UserProfilePortlet) createPortlet(dbKey, null);
_profile.getPortlets().put(rootPortlet.getKey(), rootPortlet);
return rootPortlet;
}
}
@Override
public boolean hasItem(WGPortlet portlet, String name) throws WGAPIException {
return ((UserProfilePortlet) portlet).getItems().containsKey(name.toLowerCase().trim()) || ((UserProfilePortlet) portlet).getItems().containsKey(name);
}
@Override
public void insertPortlet(WGPortlet wgPortlet) throws WGAPIException {
UserProfilePortlet portlet = (UserProfilePortlet) wgPortlet;
_profile.getPortlets().put(portlet.getKey(), portlet);
}
@Override
public void prepareNewProfile(WGUserProfile profile) throws WGAPIException {
}
@Override
public void removeItem(WGPortlet portlet, String name) throws WGAPIException {
UserProfilePortlet v5Portlet = (UserProfilePortlet) portlet;
Hibernate.initialize(v5Portlet.getItems());
Map<String, UserProfileItem> items = v5Portlet.getItems();
String lcName = name.toLowerCase().trim();
if (items.containsKey(lcName)) {
v5Portlet.getItems().remove(lcName);
}
else if (items.containsKey(name)) {
v5Portlet.getItems().remove(name);
}
}
@Override
public void removePortlet(WGPortlet portlet) throws WGAPIException {
if (portlet.isRoot()) {
throw new WGIllegalArgumentException("Cannot remove the root portlet");
}
// First unregister child portlets
Iterator<WGPortlet> children = getChildPortlets(portlet).iterator();
while (children.hasNext()) {
WGPortlet child = (WGPortlet) children.next();
removePortlet(child);
}
// Cleanup all items
clearItems(portlet);
Hibernate.initialize(_profile.getPortlets());
UserProfilePortlet upPortlet = _profile.getPortlets().remove(portlet.getKey());
_parent.getSession().delete(upPortlet);
}
@Override
public void setItemValue(WGPortlet wgPortlet, String name, Object value) throws WGAPIException {
name = name.toLowerCase().trim();
UserProfilePortlet portlet = (UserProfilePortlet) wgPortlet;
UserProfileItem item = ((UserProfilePortlet) portlet).getItems().get(name);
if (item == null) {
item = new UserProfileItem();
item.setParentprofile(_profile);
item.setPortlet(portlet);
item.setName(name);
portlet.getItems().put(name, item);
}
WGDocumentImpl.writeItemValue(_parent, item, value);
}
@Override
public void updatePortlet(WGPortlet portlet) throws WGAPIException {
// Nothing to do since the portlet object is already persistent and will get saved in its current state
// Theoretically a problem if one changes portlet data, does NOT call updatePortlet and then saves the profile = The portlet is saved though updatePortlet never got called
}
@Override
public WGPortlet createPortlet(String appDb, WGPortlet parent) {
UserProfilePortlet portlet = new UserProfilePortlet();
portlet.setParentprofile(_profile);
portlet.setParentportlet((UserProfilePortlet) parent);
if (parent != null) {
portlet.setKey(UIDGenerator.generateUID());
}
else {
String rootKey = (appDb.length() > 32 ? appDb.substring(0, 32) : appDb);
portlet.setKey(rootKey);
}
portlet.setAppdb(appDb);
portlet.setItems(new HashMap<String,UserProfileItem>());
portlet.setExtensionData(new HashMap<String, ExtensionData>());
return portlet;
}
@Override
public Set<String> getAppDbs() {
List<String> appDbList = Groq.selectFrom(_profile.getPortlets().values()).listProperty("appdb");
Set<String> apps = new HashSet<String>();
apps.addAll(appDbList);
return apps;
}
}