Skip to content

Commit

Permalink
For #85 - handle pictures upload JSON result
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalidze committed May 25, 2015
1 parent 2d23ec2 commit 4371dba
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
53 changes: 51 additions & 2 deletions src/main/java/org/traccar/web/client/view/DeviceMarkersDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package org.traccar.web.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsonUtils;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.resources.client.ClientBundle;
Expand Down Expand Up @@ -52,6 +54,7 @@
import org.traccar.web.client.model.BaseStoreHandlers;
import org.traccar.web.shared.model.DeviceIcon;
import org.traccar.web.shared.model.DeviceIconType;
import org.traccar.web.shared.model.Picture;
import org.traccar.web.shared.model.Position;

import java.util.ArrayList;
Expand Down Expand Up @@ -204,6 +207,40 @@ public void onSuccess(List<DeviceIcon> loaded) {
}
}

static class PictureJso extends JavaScriptObject {
protected PictureJso() {}

public final native int getId() /*-{ return this.id; }-*/;
public final native int getWidth() /*-{ return this.width; }-*/;
public final native int getHeight() /*-{ return this.height; }-*/;

final Picture toPicture() {
Picture p = new Picture();
p.setId(getId());
p.setWidth(getWidth());
p.setHeight(getHeight());
return p;
}
}

static class DeviceIconJso extends JavaScriptObject {
protected DeviceIconJso() {}

public final native int getId() /*-{ return this.id; }-*/;
public final native PictureJso getDefaultIcon() /*-{ return this.defaultIcon; }-*/;
public final native PictureJso getSelectedIcon() /*-{ return this.selectedIcon; }-*/;
public final native PictureJso getOfflineIcon() /*-{ return this.offlineIcon; }-*/;

final DeviceIcon toDeviceIcon() {
DeviceIcon di = new DeviceIcon();
di.setId(getId());
di.setDefaultIcon(getDefaultIcon().toPicture());
di.setSelectedIcon(getSelectedIcon().toPicture());
di.setOfflineIcon(getOfflineIcon().toPicture());
return di;
}
}

RpcProxy<Object, List<Marker>> hybridProxy = new RpcProxy<Object, List<Marker>>() {
@Override
public void load(Object loadConfig, AsyncCallback<List<Marker>> callback) {
Expand All @@ -213,7 +250,7 @@ public void load(Object loadConfig, AsyncCallback<List<Marker>> callback) {

Marker selected;

public DeviceMarkersDialog(DeviceIconType selectedIcon, DeviceMarkerHandler handler) {
public DeviceMarkersDialog(final DeviceIconType selectedIcon, DeviceMarkerHandler handler) {
this.handler = handler;

ModelKeyProvider<Marker> keyProvider = new ModelKeyProvider<Marker>() {
Expand Down Expand Up @@ -291,7 +328,19 @@ public void onSelection(SelectionEvent<Marker> event) {
form.addSubmitCompleteHandler(new SubmitCompleteEvent.SubmitCompleteHandler() {
@Override
public void onSubmitComplete(SubmitCompleteEvent event) {
new LogViewDialog(event.getResults()).show();
String s = event.getResults();
if (s.indexOf('>') >= 0) {
s = s.substring(s.indexOf('>') + 1, s.lastIndexOf('<'));
}
if (JsonUtils.safeToEval(s)) {
DeviceIconJso deviceIconJso = JsonUtils.safeEval(s);
Marker marker = new DatabaseMarker(deviceIconJso.toDeviceIcon());
selected = marker;
store.add(0, marker);
fileToImport.reset();
} else {
new LogViewDialog(event.getResults()).show();
}
}
});

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/traccar/web/server/model/PicturesServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
*/
package org.traccar.web.server.model;

import com.google.gson.Gson;
import com.google.inject.persist.Transactional;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.traccar.web.shared.model.DeviceIcon;
import org.traccar.web.shared.model.Picture;
import org.traccar.web.shared.model.PictureType;

Expand Down Expand Up @@ -116,6 +118,18 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
picture.setMimeType(mimeType);
picture.setData(FileUtils.readFileToByteArray(file));
entityManager.get().persist(picture);

Gson gson = GsonUtils.create();
// create new device icon when 'MARKER' was uploaded
if (pictureType == PictureType.MARKER) {
DeviceIcon deviceIcon = new DeviceIcon();
deviceIcon.setDefaultIcon(picture);
deviceIcon.setSelectedIcon(picture);
deviceIcon.setOfflineIcon(picture);
entityManager.get().persist(deviceIcon);

resp.getWriter().write(gson.toJson(deviceIcon));
}
}
} catch (FileUploadException fue) {
logger.log(Level.WARNING, fue.getLocalizedMessage(), fue);
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/traccar/web/shared/model/DeviceIcon.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/
package org.traccar.web.shared.model;

import com.google.gwt.user.client.rpc.GwtTransient;
import com.google.gson.annotations.Expose;
import com.google.gwt.user.client.rpc.IsSerializable;

import javax.persistence.*;

@Entity
@Table(name = "device_icons")
public class DeviceIcon implements IsSerializable {
@Expose
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, updatable = false, unique = true)
Expand All @@ -36,6 +37,7 @@ public void setId(long id) {
this.id = id;
}

@Expose
@ManyToOne
@JoinColumn(foreignKey = @ForeignKey(name = "device_icons_fkey_def_icon_id"))
private Picture defaultIcon;
Expand All @@ -48,6 +50,7 @@ public void setDefaultIcon(Picture defaultIcon) {
this.defaultIcon = defaultIcon;
}

@Expose
@ManyToOne
@JoinColumn(foreignKey = @ForeignKey(name = "device_icons_fkey_sel_icon_id"))
private Picture selectedIcon;
Expand All @@ -60,6 +63,7 @@ public void setSelectedIcon(Picture selectedIcon) {
this.selectedIcon = selectedIcon;
}

@Expose
@ManyToOne
@JoinColumn(foreignKey = @ForeignKey(name = "device_icons_fkey_off_icon_id"))
private Picture offlineIcon;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/traccar/web/shared/model/Picture.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.traccar.web.shared.model;

import com.google.gson.annotations.Expose;
import com.google.gwt.user.client.rpc.GwtTransient;
import com.google.gwt.user.client.rpc.IsSerializable;

Expand All @@ -25,6 +26,7 @@
public class Picture implements IsSerializable {
public static final String URL_PREFIX = "/traccar/p/";

@Expose
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, updatable = false, unique = true)
Expand Down Expand Up @@ -63,7 +65,9 @@ public void setData(byte[] data) {
this.data = data;
}

@Expose
private int width;
@Expose
private int height;
private String mimeType;

Expand Down

0 comments on commit 4371dba

Please sign in to comment.