[gogoego] r3710 committed - mail settings

1 view
Skip to first unread message

codesite...@google.com

unread,
Jul 19, 2010, 5:01:52 PM7/19/10
to gog...@googlegroups.com
Revision: 3710
Author: carl....@solertium.com
Date: Mon Jul 19 13:50:01 2010
Log: mail settings
http://code.google.com/p/gogoego/source/detail?r=3710

Added:

/trunk/modules/com.solertium.gogoego.client/src/com/solertium/gogoego/client/gwt/settings/mail

/trunk/modules/com.solertium.gogoego.client/src/com/solertium/gogoego/client/gwt/settings/mail/GeneralSettingsPanel.java

/trunk/modules/com.solertium.gogoego.client/src/com/solertium/gogoego/client/gwt/settings/mail/GenericSettingsPanel.java

/trunk/modules/com.solertium.gogoego.server/src/com/solertium/gogoego/server/lib/settings/mail

/trunk/modules/com.solertium.gogoego.server/src/com/solertium/gogoego/server/lib/settings/mail/MailSettingsResource.java

/trunk/modules/org.gogoego.api/src/org/gogoego/api/mail/LocalMailSettings.java

=======================================
--- /dev/null
+++
/trunk/modules/com.solertium.gogoego.client/src/com/solertium/gogoego/client/gwt/settings/mail/GeneralSettingsPanel.java
Mon Jul 19 13:50:01 2010
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2007-2008 Solertium Corporation
+ *
+ * This file is part of the open source GoGoEgo project.
+ *
+ * GoGoEgo 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.
+ *
+ * GoGoEgo 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 GoGoEgo. If not, see http://www.gnu.org/licenses/.
+ *
+ * Unless you have been granted a different license in writing by the
+ * copyright holders for GoGoEgo, only the GNU General Public License
+ * grants you rights to modify or redistribute this code.
+ */
+package com.solertium.gogoego.client.gwt.settings.mail;
+
+import java.util.Map;
+
+public class GeneralSettingsPanel extends GenericSettingsPanel {
+
+ public GeneralSettingsPanel() {
+ super("generic", "general.xml");
+ }
+
+ protected void renderForm(Map<String, String> initValues) {
+ form.add(createConfigurationFieldSet(initValues));
+ form.add(createAccountFieldSet(initValues));
+ }
+
+}
=======================================
--- /dev/null
+++
/trunk/modules/com.solertium.gogoego.client/src/com/solertium/gogoego/client/gwt/settings/mail/GenericSettingsPanel.java
Mon Jul 19 13:50:01 2010
@@ -0,0 +1,283 @@
+/*
+ * Copyright (C) 2007-2008 Solertium Corporation
+ *
+ * This file is part of the open source GoGoEgo project.
+ *
+ * GoGoEgo 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.
+ *
+ * GoGoEgo 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 GoGoEgo. If not, see http://www.gnu.org/licenses/.
+ *
+ * Unless you have been granted a different license in writing by the
+ * copyright holders for GoGoEgo, only the GNU General Public License
+ * grants you rights to modify or redistribute this code.
+ */
+package com.solertium.gogoego.client.gwt.settings.mail;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.extjs.gxt.ui.client.Events;
+import com.extjs.gxt.ui.client.Style.LayoutRegion;
+import com.extjs.gxt.ui.client.Style.Scroll;
+import com.extjs.gxt.ui.client.event.FormEvent;
+import com.extjs.gxt.ui.client.event.Listener;
+import com.extjs.gxt.ui.client.event.SelectionListener;
+import com.extjs.gxt.ui.client.event.ToolBarEvent;
+import com.extjs.gxt.ui.client.widget.Info;
+import com.extjs.gxt.ui.client.widget.LayoutContainer;
+import com.extjs.gxt.ui.client.widget.form.Field;
+import com.extjs.gxt.ui.client.widget.form.FieldSet;
+import com.extjs.gxt.ui.client.widget.form.FormPanel;
+import com.extjs.gxt.ui.client.widget.form.NumberField;
+import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
+import com.extjs.gxt.ui.client.widget.form.SimpleComboValue;
+import com.extjs.gxt.ui.client.widget.form.TextField;
+import com.extjs.gxt.ui.client.widget.form.FormPanel.Method;
+import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
+import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
+import com.extjs.gxt.ui.client.widget.layout.FillLayout;
+import com.extjs.gxt.ui.client.widget.layout.FormLayout;
+import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
+import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
+import com.extjs.gxt.ui.client.widget.toolbar.TextToolItem;
+import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
+import com.solertium.gogoego.client.base.gwt.Constants;
+import com.solertium.gogoego.client.base.gwt.utils.GoGoNativeDocument;
+import com.solertium.lwxml.shared.GWTNotFoundException;
+import com.solertium.lwxml.shared.GenericCallback;
+import com.solertium.lwxml.shared.NativeDocument;
+import com.solertium.lwxml.shared.NativeElement;
+import com.solertium.lwxml.shared.NativeNodeList;
+import com.solertium.util.extjs.client.WindowUtils;
+import com.solertium.util.gwt.ui.DrawsLazily;
+
+public abstract class GenericSettingsPanel extends LayoutContainer
implements DrawsLazily {
+
+ protected final FormPanel form;
+ protected final String scope;
+ protected final String filename;
+
+ public GenericSettingsPanel(String scope, String filename) {
+ super();
+ setLayout(new FillLayout());
+ this.scope = scope;
+ this.filename = filename;
+
+ form = createFormPanel();
+ form.addListener(Events.Submit, new Listener<FormEvent>() {
+ public void handleEvent(FormEvent be) {
+ Info.display("Success", "Configuration saved.");
+ }
+ });
+ }
+
+ protected abstract void renderForm(Map<String, String> initValues);
+
+ public void draw(final DrawsLazily.DoneDrawingCallback callback) {
+ load(new GenericCallback<NativeDocument>() {
+ public void onFailure(Throwable caught) {
+ WindowUtils.errorAlert("Could not load, please try again later.");
+ callback.isDrawn();
+ }
+ public void onSuccess(NativeDocument result) {
+ final Map<String, String> initValues = new HashMap<String, String>();
+ final NativeNodeList nodes =
result.getDocumentElement().getElementsByTagName("field");
+ for (int i = 0; i < nodes.getLength(); i++) {
+ final NativeElement field = nodes.elementAt(i);
+ initValues.put(field.getAttribute("name"), field.getTextContent());
+ }
+
+ renderForm(initValues);
+
+ final ToolBar bar = new ToolBar();
+ addToolItems(bar);
+ bar.add(new SeparatorToolItem());
+ bar.add(new TextToolItem("Save", new SelectionListener<ToolBarEvent>()
{
+ public void componentSelected(ToolBarEvent ce) {
+ if (form.isValid()) {
+ for (Field<?> f : form.getFields())
+ f.clearInvalid();
+ form.submit();
+ }
+ else
+ WindowUtils.errorAlert("Please fill in all required fields.");
+ }
+ }));
+
+ final LayoutContainer container = new LayoutContainer();
+ container.setLayout(new BorderLayout());
+
+ container.add(bar, new BorderLayoutData(LayoutRegion.NORTH, 25, 25,
25));
+ container.add(form, new BorderLayoutData(LayoutRegion.CENTER));
+
+ add(container);
+
+ callback.isDrawn();
+ }
+ });
+ }
+
+ protected void addToolItems(ToolBar bar) {
+ bar.add(new TextToolItem("Reset", new SelectionListener<ToolBarEvent>() {
+ public void componentSelected(ToolBarEvent ce) {
+ form.reset();
+ }
+ }));
+ bar.add(new FillToolItem());
+ bar.add(new TextToolItem("Disable", new
SelectionListener<ToolBarEvent>() {
+ public void componentSelected(ToolBarEvent ce) {
+ WindowUtils.confirmAlert("Confirm", "Are you sure you want to disable
this configuration? All settings for this configuration will be deleted.",
new WindowUtils.MessageBoxListener() {
+ public void onNo() { }
+ public void onYes() {
+ final NativeDocument document =
GoGoNativeDocument.newNativeDocument();
+ document.delete(Constants.ADMIN_MOUNT + "/settings/"+scope+"/" +
filename, new GenericCallback<String>() {
+ public void onFailure(Throwable caught) {
+ if (caught instanceof GWTNotFoundException)
+ onSuccess(null);
+ else
+ WindowUtils.errorAlert("Could not disable, please try again
later.");
+ }
+ public void onSuccess(String result) {
+ //fireEvent(CommonConstants.EVENT_DELETE);
+ Info.display("Success", "Configuration deleted and disabled.");
+ form.reset();
+ }
+ });
+ }
+ });
+ }
+ }));
+ }
+
+ protected void load(final GenericCallback<NativeDocument> callback) {
+ final NativeDocument document = GoGoNativeDocument.newNativeDocument();
+ document.get(Constants.ADMIN_MOUNT + "/settings/"+scope+"/" + filename,
new GenericCallback<String>() {
+ public void onFailure(Throwable caught) {
+ callback.onFailure(caught);
+ }
+ public void onSuccess(String result) {
+ callback.onSuccess(document);
+ }
+ });
+ }
+
+ protected FieldSet createConfigurationFieldSet(final Map<String, String>
initValues) {
+ final FieldSet config = new FieldSet();
+ config.setLayout(new FormLayout());
+ config.setHeading("Configuration");
+ config.add(createTextField("server", initValues.get("server"), "SMTP
Server", true));
+ config.add(createNumberField("port", initValues.get("port"), "Port",
true));
+
+ final SimpleComboBox<String> box = new SimpleComboBox<String>();
+ box.add("false");
+ box.add("true");
+ box.setName("ssl");
+ box.setFieldLabel("Requires SSL?");
+ box.setEditable(false);
+ box.setForceSelection(true);
+ box.setAllowBlank(false);
+
+ final String value = initValues.get("ssl");
+ if (value != null) {
+ final List<SimpleComboValue<String>> selected = new
ArrayList<SimpleComboValue<String>>();
+ for (SimpleComboValue<String> current : box.getStore().getModels())
+ if (value.equals(current.getValue()))
+ selected.add(current);
+ box.setSelection(selected);
+ }
+
+ config.add(box);
+
+ return config;
+ }
+
+ protected FieldSet createAccountFieldSet(final Map<String, String>
initValues) {
+ final FieldSet acct = new FieldSet();
+ acct.setLayout(new FormLayout());
+ acct.setHeading("Account Information");
+
+ final TextField<String> account = createTextField("account",
initValues.get("account"), "E-mail Acccount", true);
+ account.setToolTip("This is the account that will be used to send mail.
<br/>The recipient will receive mail from this address");
+ acct.add(account);
+ acct.add(createPasswordField("password",
initValues.get("password"), "Password", false));
+
+ return acct;
+ }
+
+ protected TextField<String> createTextField(String name, String value,
String label, boolean isRequired) {
+ final TextField<String> field = new TextField<String>();
+ field.setName(name);
+ field.setFieldLabel(label);
+ if (value != null)
+ field.setValue(value);
+ field.setAllowBlank(!isRequired);
+
+ return field;
+ }
+
+ protected TextField<String> createPasswordField(String name, String
value, String label, boolean isRequired) {
+ final TextField<String> field = createTextField(name, value, label,
isRequired);
+ field.setPassword(true);
+
+ return field;
+ }
+
+ protected NumberField createNumberField(String name, String value, String
label, boolean isRequired) {
+ final NumberField field = new NumberField();
+ field.setAllowDecimals(false);
+ field.setAllowNegative(false);
+ field.setName(name);
+ field.setFieldLabel(label);
+ field.setAllowBlank(!isRequired);
+ if (value != null) {
+ try {
+ field.setValue(Integer.parseInt(value));
+ } catch (NumberFormatException e) {
+ //Do nothing
+ }
+ }
+
+ return field;
+ }
+
+ protected SimpleComboBox<String> createComboBox(String name, String
value, String label, boolean isRequired, String... options) {
+ final SimpleComboBox<String> box = new SimpleComboBox<String>();
+ box.setName(name);
+ box.setFieldLabel(label);
+ box.setEditable(false);
+ box.setForceSelection(true);
+ box.setAllowBlank(!isRequired);
+ for (String option : options)
+ box.add(option);
+ if (value != null) {
+ final List<SimpleComboValue<String>> selected = new
ArrayList<SimpleComboValue<String>>();
+ for (SimpleComboValue<String> current : box.getStore().getModels())
+ if (value.equals(current.getValue()))
+ selected.add(current);
+ box.setSelection(selected);
+ }
+ return box;
+ }
+
+ protected FormPanel createFormPanel() {
+ final FormPanel panel = new FormPanel();
+ panel.setAction(Constants.ADMIN_MOUNT + "/settings/"+scope+"/" +
filename);
+ panel.setMethod(Method.POST);
+ panel.setHeaderVisible(false);
+ panel.setBodyBorder(false);
+ panel.setScrollMode(Scroll.AUTO);
+
+ return panel;
+ }
+}
=======================================
--- /dev/null
+++
/trunk/modules/com.solertium.gogoego.server/src/com/solertium/gogoego/server/lib/settings/mail/MailSettingsResource.java
Mon Jul 19 13:50:01 2010
@@ -0,0 +1,191 @@
+/*
+ * Copyright (C) 2007-2008 Solertium Corporation
+ *
+ * This file is part of the open source GoGoEgo project.
+ *
+ * GoGoEgo 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.
+ *
+ * GoGoEgo 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 GoGoEgo. If not, see http://www.gnu.org/licenses/.
+ *
+ * Unless you have been granted a different license in writing by the
+ * copyright holders for GoGoEgo, only the GNU General Public License
+ * grants you rights to modify or redistribute this code.
+ */
+package com.solertium.gogoego.server.lib.settings.mail;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.gogoego.api.plugins.GoGoEgo;
+import org.gogoego.api.utils.DocumentUtils;
+import org.restlet.Context;
+import org.restlet.data.Form;
+import org.restlet.data.MediaType;
+import org.restlet.data.Request;
+import org.restlet.data.Response;
+import org.restlet.data.Status;
+import org.restlet.ext.xml.DomRepresentation;
+import org.restlet.representation.InputRepresentation;
+import org.restlet.representation.Representation;
+import org.restlet.representation.StringRepresentation;
+import org.restlet.representation.Variant;
+import org.restlet.resource.Resource;
+import org.restlet.resource.ResourceException;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+import com.solertium.util.BaseDocumentUtils;
+import com.solertium.util.NodeCollection;
+import com.solertium.vfs.VFS;
+import com.solertium.vfs.VFSPath;
+import com.solertium.vfs.VFSPathToken;
+
+/**
+ * SimpleSettingsResource.java
+ *
+ *
+ *
+ * @author <a href="mailto:carl....@solertium.com">Carl Scott</a>, <a
+ * href="http://www.solertium.com">Solertium Corporation</a>
+ */
+public class MailSettingsResource extends Resource {
+
+ private static final VFSPath MAIL_SETTINGS_ROOT =
+ new VFSPath("/(SYSTEM)/settings/mail");
+
+ public static final String FILENAME_ATTR = "filename";
+
+ private final VFS vfs;
+ private final VFSPath uri;
+
+ /**
+ * @param context
+ * @param request
+ * @param response
+ */
+ public MailSettingsResource(Context context, Request request, Response
response) {
+ super(context, request, response);
+ setModifiable(true);
+
+ VFSPathToken file = null;
+ try {
+ file = new VFSPathToken((String)
request.getAttributes().get(FILENAME_ATTR));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ vfs = GoGoEgo.get().getFromContext(context).getVFS();
+
+ if (file == null)
+ uri = null;
+ else
+ uri = getRootUri().child(file);
+
+ getVariants().add(new Variant(MediaType.TEXT_XML));
+ }
+
+ protected VFSPath getRootUri() {
+ return MAIL_SETTINGS_ROOT;
+ }
+
+ public Representation represent(Variant variant) throws ResourceException
{
+ if (uri == null)
+ throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND);
+
+ if (vfs.exists(uri)) {
+ try {
+ return new InputRepresentation(vfs.getInputStream(uri),
variant.getMediaType());
+ } catch (IOException e) {
+ throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
+ }
+ } else {
+ return new StringRepresentation("<root></root>",
variant.getMediaType());
+ }
+ }
+
+ public void acceptRepresentation(Representation entity) throws
ResourceException {
+ if (uri == null)
+ throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND);
+
+ final Document document;
+ if (vfs.exists(uri)) {
+ try {
+ document = vfs.getMutableDocument(uri);
+ } catch (IOException e) {
+ throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
+ }
+ } else {
+ document = BaseDocumentUtils.impl.newDocument();
+ document.appendChild(document.createElement("root"));
+ }
+
+ final Map<String, Node> map = new HashMap<String, Node>();
+ final NodeCollection nodes = new
NodeCollection(document.getDocumentElement().getChildNodes());
+ for (Node node : nodes)
+ if (node.getNodeName().equals("field"))
+ map.put(BaseDocumentUtils.impl.getAttribute(node, "name"), node);
+
+ final Form form = new Form(entity);
+ for (String name : form.getNames()) {
+ if (map.containsKey(name))
+ map.get(name).setTextContent(form.getFirstValue(name));
+ else {
+ final Element newSetting = document.createElement("field");
+ newSetting.setAttribute("name", name);
+ newSetting.setTextContent(form.getFirstValue(name));
+
+ document.getDocumentElement().appendChild(newSetting);
+ }
+ }
+
+ DocumentUtils.writeVFSFile(uri.toString(), vfs, document);
+ }
+
+ public void handlePut() {
+ try {
+ storeRepresentation(getRequest().getEntity());
+ } catch (ResourceException e) {
+ getResponse().setStatus(e.getStatus());
+ }
+ }
+
+ public void storeRepresentation(Representation entity) throws
ResourceException {
+ if (uri == null)
+ throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND);
+
+ final Document document;
+ try {
+ document = new DomRepresentation(entity).getDocument();
+ } catch (Exception e) {
+ throw new ResourceException(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY,
e);
+ }
+
+ if (DocumentUtils.writeVFSFile(uri.toString(), vfs, document))
+ getResponse().setStatus(Status.SUCCESS_CREATED);
+ else
+ throw new ResourceException(Status.SERVER_ERROR_INTERNAL);
+ }
+
+ public void removeRepresentations() throws ResourceException {
+ if (uri == null || !vfs.exists(uri))
+ throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND);
+
+ try {
+ vfs.delete(uri);
+ } catch (IOException e) {
+ throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
+ }
+ }
+
+}
=======================================
--- /dev/null
+++
/trunk/modules/org.gogoego.api/src/org/gogoego/api/mail/LocalMailSettings.java
Mon Jul 19 13:50:01 2010
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 2007-2008 Solertium Corporation
+ *
+ * This file is part of the open source GoGoEgo project.
+ *
+ * GoGoEgo 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.
+ *
+ * GoGoEgo 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 GoGoEgo. If not, see http://www.gnu.org/licenses/.
+ *
+ * Unless you have been granted a different license in writing by the
+ * copyright holders for GoGoEgo, only the GNU General Public License
+ * grants you rights to modify or redistribute this code.
+ */
+package org.gogoego.api.mail;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import com.solertium.util.ElementCollection;
+import com.solertium.vfs.VFS;
+import com.solertium.vfs.VFSPath;
+
+public class LocalMailSettings {
+
+ private final Map<String, String> map;
+
+ public LocalMailSettings() {
+ map = new HashMap<String, String>();
+ }
+
+ public LocalMailSettings(VFS vfs) throws InstantiationException {
+ this();
+
+ final VFSPath path = new VFSPath("/(SYSTEM)/settings/mail/general.xml");
+
+ final Document document;
+ try {
+ document = vfs.getDocument(path);
+ } catch (IOException e) {
+ throw new InstantiationException(e.getMessage());
+ } catch (NullPointerException e) {
+ throw new IllegalArgumentException(e);
+ }
+
+ final ElementCollection nodes = new
ElementCollection(document.getDocumentElement().getElementsByTagName(
+ "field"));
+ for (Element node : nodes)
+ if (!"".equals(node.getTextContent()))
+ map.put(node.getAttribute("name"), node.getTextContent());
+ }
+
+ public String getSubject() {
+ return getProperty("subject", "(no subject)");
+ }
+
+ public String getReplyToAddress() {
+ return getProperty("replyto");
+ }
+
+ public String getAccount() {
+ return getProperty("account");
+ }
+
+ public String getBCC() {
+ return getProperty("bcc");
+ }
+
+ public String getCC() {
+ return getProperty("cc");
+ }
+
+ public String getRecipient() {
+ return getProperty("to", getAccount());
+ }
+
+ public int getPort() {
+ try {
+ return Integer.parseInt(getProperty("port", "25"));
+ } catch (NumberFormatException e) {
+ return 25;
+ }
+ }
+
+ public boolean isSsl() {
+ return "true".equals(getProperty("ssl"));
+ }
+
+ public String getPassword() {
+ return getProperty("password");
+ }
+
+ public String getServer() {
+ return getProperty("server");
+ }
+
+ public String getProperty(String key) {
+ return map.get(key);
+ }
+
+ public String getProperty(String key, String defaultValue) {
+ return map.containsKey(key) ? map.get(key) : defaultValue;
+ }
+
+ public String toString() {
+ return map.toString();
+ }
+}

Reply all
Reply to author
Forward
0 new messages