/*******************************************************************************
* 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;
import java.util.Iterator;
import de.innovationgate.webgate.api.WGAPIException;
import de.innovationgate.webgate.api.WGArea;
import de.innovationgate.webgate.api.WGContentType;
import de.innovationgate.webgate.api.WGDatabase;
import de.innovationgate.webgate.api.WGDocument;
import de.innovationgate.webgate.api.WGLanguage;
import de.innovationgate.webgate.api.WGStructEntry;
import de.innovationgate.wgpublisher.WGPDispatcher;
import de.innovationgate.wgpublisher.webtml.utils.TMLException;
public class CreatePage extends Base {
private String _area;
private String _contenttype;
private String _message;
private class TagResult {
private String _cssClass = "BI-create";
private String _area;
private String _structKey;
private String _contentType;
private String _message;
public TagResult(String message, WGArea area) throws WGAPIException {
_area = area.getName();
_message = message;
}
public TagResult(String message, WGStructEntry entry) throws WGAPIException {
_structKey = (String) entry.getStructKey();
_message = message;
}
public String toHTML() {
StringBuffer html = new StringBuffer();
html.append("<span class=\"" + _cssClass + "\" style=\"display:none\">");
html.append("<span style=\"display:none\">");
html.append("{");
if (_area != null) {
html.append("area:'" + _area + "'");
} else if (_structKey != null) {
html.append("structkey:'" + _structKey + "'");
}
if (_contentType != null) {
html.append(", contenttype:'" + _contenttype + "'");
}
html.append("}");
html.append("</span>");
html.append("<span>");
html.append(_message);
html.append("</span>");
html.append("</span>");
return html.toString();
}
public void setContentType(String contentType) {
_contentType = contentType;
}
}
/*
* (non-Javadoc)
* @see de.innovationgate.wgpublisher.webtml.Base#tmlEndTag()
*/
public void tmlEndTag() throws TMLException, WGAPIException {
// check if BI is enabled
if (!WGPDispatcher.isBrowserInterface(getPageContext().getSession())) {
setResultOutput(false);
return;
}
// check db access - minimum author
if (!(getTMLContext().db().getSessionContext().getAccessLevel() >= WGDatabase.ACCESSLEVEL_AUTHOR)) {
setResultOutput(false);
return;
}
// Check if we are allowed to edit any language
Iterator langs = getTMLContext().db().getLanguages().values().iterator();
boolean langFound = false;
while (langs.hasNext()) {
WGLanguage lang = (WGLanguage) langs.next();
if (lang.mayCreateContent()) {
langFound = true;
break;
}
}
if (langFound == false) {
return;
}
if (getArea() != null) {
// a new root content should be created
WGArea area = getTMLContext().db().getArea(getArea());
// check access
if (area.mayEditAreaChildren() && contentTypeAllowed(area)) {
TagResult result = new TagResult(getMessage(), area);
result.setContentType(getContenttype());
setResult(result.toHTML());
} else {
setResultOutput(false);
return;
}
} else {
// use current context as parent for content creation
WGStructEntry structEntry = getTMLContext().getcontent().getStructEntry();
if (structEntry != null) {
// check access
if ((structEntry.mayEditChildren() == null) && contentTypeAllowed(structEntry)) {
TagResult result = new TagResult(getMessage(), structEntry);
result.setContentType(getContenttype());
setResult(result.toHTML());
} else {
setResultOutput(false);
return;
}
} else {
setResultOutput(false);
return;
}
}
}
public String getMessage() {
return getTagAttributeValue("message", _message, null);
}
public void setMessage(String message) {
_message = message;
}
private boolean contentTypeAllowed(WGDocument doc) throws WGAPIException {
// With specified content type
if (getContenttype() != null) {
WGContentType contentType = getTMLContext().db().getContentType(getContenttype());
if (contentType != null) {
return contentType.mayCreateContent() && contentType.mayCreateChildEntry(doc);
}
else {
addWarning("Contenttype '" + getContenttype() + "' could not be found in database '" + getTMLContext().db().getDbReference() + "'.");
return false;
}
}
// no content type specified. Look if any content type is usable at this position
else {
Iterator contentTypes = getTMLContext().db().getContentTypes().iterator();
boolean ctFound = false;
while (contentTypes.hasNext()) {
WGContentType ct = (WGContentType) contentTypes.next();
if (ct.mayCreateContent() && ct.mayCreateChildEntry(doc)) {
ctFound = true;
break;
}
}
return ctFound;
}
}
public String getArea() {
return getTagAttributeValue("area", _area, null);
}
public void setArea(String area) {
_area = area;
}
public String getContenttype() {
return getTagAttributeValue("contentType", _contenttype, null);
}
public void setContenttype(String contenttype) {
_contenttype = contenttype;
}
}