Permalink
Browse files

GS-44: implemented the user preference functionality.

  • Loading branch information...
1 parent b701674 commit 9aad9f7dfc796eb5ea3639f9c6ba3b94b4d64b8a @jeffyu jeffyu committed Mar 16, 2013
@@ -23,6 +23,7 @@
import org.overlord.gadgets.server.model.WidgetPreference;
import java.util.List;
+import java.util.Map;
/**
* @author: Jeff Yu
@@ -55,6 +56,8 @@
void removeWidget(long widgetId);
void updateWidgetPreference(long widgetId, List<WidgetPreference> prefs);
+
+ Map<String, String> getWidgetPreference(long widgetId);
Widget getWidgetById(long widgetId);
}
@@ -27,7 +27,9 @@
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
/**
* @author: Jeff Yu
@@ -195,4 +197,19 @@ public void updateWidgetPreference(long widgetId,
entityManager.getTransaction().commit();
}
+ public Map<String, String> getWidgetPreference(long widgetId) {
+ Map<String, String> result = new HashMap<String, String>();
+ if (!entityManager.getTransaction().isActive()) {
+ entityManager.getTransaction().begin();
+ }
+ Widget widget = entityManager.find(Widget.class, widgetId);
+ if (widget.getPrefs() != null) {
+ for (WidgetPreference pref : widget.getPrefs()) {
+ result.put(pref.getName(), pref.getValue());
+ }
+ }
+ entityManager.getTransaction().commit();
+ return result;
+ }
+
}
@@ -78,5 +78,9 @@ public static String getRemovePageURL(long pageId) {
public static String updatePreferenceURL(long widgetId) {
return urlBase + "rs/users/widget/"+widgetId+"/update";
}
+
+ public static String getPreferenceValuesURL(long widgetId) {
+ return urlBase + "rs/users/widget/" + widgetId;
+ }
}
@@ -19,11 +19,14 @@
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
+import com.google.gwt.core.client.JsArrayString;
import org.overlord.gadgets.web.shared.dto.*;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
/**
* @author: Jeff Yu
@@ -133,5 +136,16 @@ public static UserModel getUserModel(String jsonValue) {
user.setDisplayName(model.get("displayName"));
return user;
}
+
+ public static Map<String, String> getPreferenceValues(String jsonValue) {
+ Map<String, String> result = new HashMap<String, String>();
+ JsArray<JSOModel> model = JSOModel.arrayFromJson(jsonValue);
+ for (int i = 0; i < model.length(); i++) {
+ JSOModel pairModel = model.get(i);
+ result.put(pairModel.get("name"), pairModel.get("value"));
+ }
+
+ return result;
+ }
}
@@ -19,8 +19,11 @@
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
+import java.util.Set;
import org.overlord.gadgets.web.client.URLBuilder;
+import org.overlord.gadgets.web.client.model.JSOParser;
import org.overlord.gadgets.web.client.util.RestfulInvoker;
import org.overlord.gadgets.web.shared.dto.UserPreference;
import org.overlord.gadgets.web.shared.dto.UserPreference.Option;
@@ -69,8 +72,12 @@
//This is the portalLayout Id.
private String portalId;
+ private String iframeId;
+
private WidgetModel wmodel;
+ private String userPreferenceValues;
+
private List<Widget> prefs = new ArrayList<Widget>();
@UiField InlineLabel minBtn;
@@ -88,8 +95,10 @@ private Portlet(final String wid, final String pid) {
widgetId = wid;
id = "portlet-" + widgetId;
this.portalId = pid;
+ this.iframeId = "iframe-" + widgetId;
initWidget(uiBinder.createAndBindUi(this));
getElement().setId(id);
+ gadgetSpec.getElement().setId(iframeId);
urlBase = getGadgetServerUrlBase();
@@ -121,7 +130,7 @@ public void onClick(ClickEvent event) {
gadgetSpec.setWidth("100%");
gadgetSpec.setHeight("90%");
gadgetSpec.getElement().setAttribute("scrolling", "auto");
- gadgetSpec.setUrl(urlBase + "gadget-server/gadgets/ifr?url=" + wmodel.getSpecUrl() + "&" + getCanvasView());
+ gadgetSpec.setUrl(urlBase + "gadget-server/gadgets/ifr?url=" + wmodel.getSpecUrl() + "&" + getCanvasView() + "&" + userPreferenceValues);
}
});
@@ -132,7 +141,7 @@ public void onClick(ClickEvent event) {
gadgetSpec.setWidth("100%");
gadgetSpec.setHeight("250px");
gadgetSpec.getElement().setAttribute("scrolling", "auto");
- gadgetSpec.setUrl(urlBase + "gadget-server/gadgets/ifr?url=" + wmodel.getSpecUrl() + "&" + getHomeView());
+ gadgetSpec.setUrl(urlBase + "gadget-server/gadgets/ifr?url=" + wmodel.getSpecUrl() + "&" + getHomeView() + "&" + userPreferenceValues);
}
});
@@ -145,18 +154,40 @@ public void onClick(ClickEvent event) {
gadgetSpec.getElement().setId(widgetId);
}
- public Portlet(WidgetModel model, int width, String portalId) {
+ public Portlet(WidgetModel model, final int width, String portalId) {
this(String.valueOf(model.getWidgetId()), portalId);
title.setText(model.getName());
wmodel = model;
- generateUserPref(model);
+ RestfulInvoker.invoke(RequestBuilder.GET, URLBuilder.getPreferenceValuesURL(Long.valueOf(widgetId)), null,
+ new RestfulInvoker.Response() {
+
+ public void onResponseReceived(Request request, Response response) {
+ Map<String, String> defaultValues = JSOParser.getPreferenceValues(response.getText());
+ generateUserPref(wmodel, defaultValues);
+ generatePreferenceValuesString(defaultValues);
+ gadgetSpec.getElement().setAttribute("scrolling", "auto");
+ gadgetSpec.getElement().setAttribute("frameborder", "0");
+ gadgetSpec.setWidth(width - 20 + "px");
+ gadgetSpec.setHeight("250px");
+ gadgetSpec.setUrl(urlBase + "gadget-server/gadgets/ifr?url=" + wmodel.getSpecUrl() + "&" + getHomeView() + "&" + userPreferenceValues);
+ }
+ });
- gadgetSpec.getElement().setAttribute("scrolling", "auto");
- gadgetSpec.getElement().setAttribute("frameborder", "0");
- gadgetSpec.setWidth(width - 20 + "px");
- gadgetSpec.setHeight("250px");
- gadgetSpec.setUrl(urlBase + "gadget-server/gadgets/ifr?url=" + model.getSpecUrl() + "&" + getHomeView());
+ }
+
+ private void generatePreferenceValuesString(Map<String, String> value) {
+ StringBuilder sbuilder = new StringBuilder();
+ int i = 1;
+ for (String key: value.keySet()) {
+ sbuilder.append(key).append("=").append(value.get(key));
+ if ( i < value.size()) {
+ sbuilder.append("&");
+ }
+ i++;
+ }
+
+ this.userPreferenceValues = sbuilder.toString();
}
private String getGadgetServerUrlBase() {
@@ -166,13 +197,13 @@ private String getGadgetServerUrlBase() {
}
- private void generateUserPref(WidgetModel model) {
+ private void generateUserPref(WidgetModel model, Map<String, String> values) {
UserPreference pref = model.getUserPreference();
int row = 0;
for (UserPreferenceSetting prefSet : pref.getData()) {
if (UserPreference.Type.STRING.equals(prefSet.getType())) {
prefTable.setWidget(row, 0, new Label(prefSet.getDisplayName()));
- Widget textBox = createTextBox(prefSet.getName(), prefSet.getDefaultValue());
+ Widget textBox = createTextBox(prefSet.getName(), getDefaultValue(values, prefSet.getName(),prefSet.getDefaultValue()));
prefs.add(textBox);
prefTable.setWidget(row, 1, textBox);
} else if (UserPreference.Type.ENUM.equals(prefSet.getType())) {
@@ -181,12 +212,12 @@ private void generateUserPref(WidgetModel model) {
for (Option option : prefSet.getEnumOptions()) {
options.add(option.getValue());
}
- Widget listBox = createSelectBox(prefSet.getName(), prefSet.getDefaultValue(), options);
+ Widget listBox = createSelectBox(prefSet.getName(), getDefaultValue(values, prefSet.getName(),prefSet.getDefaultValue()), options);
prefs.add(listBox);
prefTable.setWidget(row, 1, listBox);
} else if (UserPreference.Type.LIST.equals(prefSet.getType())) {
prefTable.setWidget(row, 0, new Label(prefSet.getDisplayName()));
- Widget listBox = createSelectBox(prefSet.getName(), prefSet.getDefaultValue(), prefSet.getListOptions());
+ Widget listBox = createSelectBox(prefSet.getName(), getDefaultValue(values, prefSet.getName(),prefSet.getDefaultValue()), prefSet.getListOptions());
prefs.add(listBox);
prefTable.setWidget(row, 1, listBox);
}
@@ -196,6 +227,13 @@ private void generateUserPref(WidgetModel model) {
}
+ private String getDefaultValue(Map<String, String> values, String name, String defaultValue) {
+ if (values.get(name) != null) {
+ return values.get(name);
+ }
+ return defaultValue;
+ }
+
private Widget createTextBox(String name, String defaultVal) {
TextBox textBox = new TextBox();
//hack, set the name to id as well for jquery retrieval.
@@ -233,9 +271,10 @@ private Widget createPrefSettingButtons() {
btnPanel.add(saveBtn);
saveBtn.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event) {
- StringBuffer sbuffer = new StringBuffer();
+ StringBuilder sbuffer = new StringBuilder();
sbuffer.append("{");
+ StringBuilder updatedValue = new StringBuilder();
for (int i = 0; i < prefs.size(); i++) {
Widget theWidget = prefs.get(i);
String name = null;
@@ -251,19 +290,27 @@ public void onClick(ClickEvent event) {
}
if (name != null) {
sbuffer.append("\"" + name + "\":\"" + value + "\"");
+ updatedValue.append(name).append("=").append(value);
if (i < prefs.size() -1) {
sbuffer.append(",");
+ updatedValue.append("&");
}
}
}
sbuffer.append("}");
Log.debug("The map value is: " + sbuffer.toString());
+ userPreferenceValues = updatedValue.toString();
+ Log.debug("The userPreferenceValues is : " + userPreferenceValues );
+
RestfulInvoker.invoke(RequestBuilder.POST, URLBuilder.updatePreferenceURL(Long.valueOf(widgetId)), sbuffer.toString(), new RestfulInvoker.Response(){
public void onResponseReceived(Request request, Response response) {
hideUserPref(id);
- gadgetSpec.setUrl(urlBase + "gadget-server/gadgets/ifr?url=" + wmodel.getSpecUrl() + "&" + getHomeView());
- reloadIFrame(gadgetSpec.getElement());
+ String newUrl = urlBase + "gadget-server/gadgets/ifr?url=" + wmodel.getSpecUrl() + "&" + getHomeView() + "&" + userPreferenceValues;
+ //gadgetSpec.setUrl(newUrl);
+ Log.debug("the new url is: " + newUrl);
+ //reloadIFrame(gadgetSpec.getElement().getId());
+ reloadGadget(gadgetSpec.getElement().getId(), newUrl);
}
});
@@ -359,10 +406,14 @@ private static native void showUserPref(String id) /*-{
$wnd.$('#' + id).find(".portlet-preference").show();
}-*/;
- private static native void reloadIFrame(Element iframeEl) /*-{
- iframeEl.contentWindow.location.reload(true);
+ private static native void reloadIFrame(String iframeId) /*-{
+ $wnd.document.getElementById(iframeId).reload(true);
}-*/;
+ private static native void reloadGadget(String iframeId, String url) /*-{
+ $wnd.reloadGadget(iframeId, url);
+ }-*/;
+
private static native void setPreference(String name, String value) /*-{
var prefs = new gadgets.Prefs();
prefs.set(name, value);
@@ -27,6 +27,7 @@
import org.overlord.gadgets.server.service.GadgetService;
import org.overlord.gadgets.server.service.UserManager;
import org.overlord.gadgets.web.shared.dto.PageModel;
+import org.overlord.gadgets.web.shared.dto.Pair;
import org.overlord.gadgets.web.shared.dto.UserModel;
import org.overlord.gadgets.web.shared.dto.UserPreference.UserPreferenceSetting;
import org.overlord.gadgets.web.shared.dto.WidgetModel;
@@ -197,6 +198,19 @@ public Response updateWidgetPreference(@PathParam("widgetId") long widgetId, Map
userManager.updateWidgetPreference(widgetId, wps);
return Response.ok().build();
}
+
+ @GET
+ @Path("widget/{widgetId}")
+ @Produces("application/json")
+ public List<Pair> getWidgetPreferenceValues(@PathParam("widgetId") long widgetId) {
+ Map<String, String> result = userManager.getWidgetPreference(widgetId);
+ List<Pair> pairs = new ArrayList<Pair>();
+ for (String key : result.keySet()) {
+ Pair pair = new Pair(key, result.get(key));
+ pairs.add(pair);
+ }
+ return pairs;
+ }
@POST
@Path("user/{userId}/page")
@@ -0,0 +1,64 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008-12, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ */
+package org.overlord.gadgets.web.shared.dto;
+
+/**
+ * @author Jeff Yu
+ * @date Mar 14, 2013
+ *
+ */
+public class Pair {
+
+ private String name;
+
+ private String value;
+
+ public Pair(String name, String value) {
+ this.name = name;
+ this.value = value;
+ }
+
+ /**
+ * @return the name
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * @param name the name to set
+ */
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ /**
+ * @return the value
+ */
+ public String getValue() {
+ return value;
+ }
+
+ /**
+ * @param value the value to set
+ */
+ public void setValue(String value) {
+ this.value = value;
+ }
+
+}
Oops, something went wrong.

0 comments on commit 9aad9f7

Please sign in to comment.