Umožněn upload obrázku podpisu uživatele. Menu Nastavení -> Uživatelské

nastavení.
refs #132
This commit is contained in:
2014-09-05 13:01:11 +02:00
parent d454c211f8
commit d108f4a070
10 changed files with 316 additions and 0 deletions
@@ -0,0 +1,18 @@
package info.bukova.isspst.storage;
import java.io.File;
public interface FileStorage {
// public String getRootPath();
public void saveFile(byte[] data, String fileName);
public void saveFile(File file, String path);
public void removeFile(String fileName);
public void moveFile(String source, String destination);
public void createDirectory(String dir);
public byte[] fileData(String fileName);
public File file(String fileName);
public boolean dirExists(String path);
public String serverPath(String fileName);
}
@@ -0,0 +1,146 @@
package info.bukova.isspst.storage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.ServletContext;
public class LocalFileStorage implements FileStorage {
private String rootPath;
private ServletContext context;
public void setRootPath(String rootPath) {
this.rootPath = rootPath;
}
public void setContext(ServletContext ctx) {
this.context = ctx;
}
private String getFullPath() {
return context.getRealPath(rootPath) + File.separator;
}
@Override
public void saveFile(byte[] data, String fileName) {
File file = new File(getFullPath() + fileName);
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
os.write(data);
os.flush();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.flush();
os.close();
}
} catch (IOException e) {
throw new StorageException("Cannot close stream", e.getCause());
}
}
}
@Override
public void saveFile(File file, String path) {
File dest = new File(getFullPath() + path + File.pathSeparator + file.getName());
FileOutputStream fos = null;
try {
fos = new FileOutputStream(dest);
fos.write(fileData(file.getName()));
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
throw new StorageException("Cannot move file: " + file.getName(), e.getCause());
} catch (IOException e) {
throw new StorageException("Cannot move file: " + file.getName(), e.getCause());
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
throw new StorageException("Cannot close stream", e.getCause());
}
}
}
}
@Override
public void removeFile(String fileName) {
File f = new File(getFullPath() + fileName);
if (!f.delete()) {
throw new StorageException("Cannot delete file: " + getFullPath()
+ fileName);
}
}
@Override
public byte[] fileData(String fileName) {
File f = new File(getFullPath() + fileName);
byte[] out = new byte[(int) f.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
fis.read(out);
fis.close();
} catch (FileNotFoundException e) {
throw new StorageException("File cannot be found: " + fileName, e.getCause());
} catch (IOException e) {
throw new StorageException("Cannot read file: " + fileName, e.getCause());
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
throw new StorageException("Cannot close stream", e.getCause());
}
}
}
return out;
}
@Override
public File file(String fileName) {
return new File(getFullPath() + fileName);
}
@Override
public void moveFile(String source, String destination) {
saveFile(new File(getFullPath() + source), destination);
}
@Override
public void createDirectory(String dir) {
File f = new File(getFullPath() + dir);
if (!f.mkdir()) {
throw new StorageException("Cannot create directory: "
+ getFullPath() + dir);
}
}
@Override
public boolean dirExists(String path) {
File f = new File(getFullPath() + path);
return f.exists() && f.isDirectory();
}
@Override
public String serverPath(String fileName) {
return context.getRealPath(rootPath + File.separator + fileName);
}
}
@@ -0,0 +1,17 @@
package info.bukova.isspst.storage;
import info.bukova.isspst.services.IsspstException;
public class StorageException extends IsspstException {
private static final long serialVersionUID = -1303880908451845756L;
public StorageException(String reason) {
super(reason);
}
public StorageException(String reason, Throwable cause) {
super(reason, cause);
}
}
@@ -48,4 +48,11 @@ public class MainMenu
Window window = (Window) Executions.createComponents("/settings/globalSettings.zul", null, null);
window.doModal();
}
@Command
public void userSettings()
{
Window window = (Window) Executions.createComponents("/settings/userSettings.zul", null, null);
window.doModal();
}
}
@@ -0,0 +1,86 @@
package info.bukova.isspst.ui.settings;
import info.bukova.isspst.data.UserSettingsData;
import info.bukova.isspst.services.users.UserService;
import info.bukova.isspst.storage.FileStorage;
import java.awt.image.RenderedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.event.UploadEvent;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Window;
public class UserSettingsVM {
@WireVariable
private UserService userService;
@WireVariable
private FileStorage storage;
private UserSettingsData settings;
@Init
public void init() {
settings = userService.getUserSettings();
}
public UserSettingsData getSettings() {
return settings;
}
@Command
@NotifyChange({"settings", "signatureImg"})
public void uploadSignature(@ContextParam(ContextType.TRIGGER_EVENT) UploadEvent upEvent) {
int i = upEvent.getMedia().getName().lastIndexOf(".");
String fileName = "SignatureImageFile_" + userService.getCurrent().getUsername() + upEvent.getMedia().getName().substring(i);
storage.saveFile(upEvent.getMedia().getByteData(), fileName);
settings.setSignatureFile(fileName);
}
@Command
@NotifyChange("signatureImg")
public void removeSignature() {
settings.setSignatureFile(null);
}
@Command
public void save(@BindingParam("window") Window window) {
userService.setUserSettings(settings);
userService.update(userService.getCurrent());
onClose();
window.detach();
}
@Command
public void onClose() {
if (userService.getUserSettings().getSignatureFile() == null && settings.getSignatureFile() != null) {
storage.removeFile(settings.getSignatureFile());
}
}
public boolean isCanSave() {
return true;
}
public RenderedImage getSignatureImg() {
if (settings.getSignatureFile() == null || settings.getSignatureFile().isEmpty()) {
return null;
}
try {
return ImageIO.read(storage.file(settings.getSignatureFile()));
} catch (IOException e) {
return null;
}
}
}