Switched to zutil UserMessageManager

This commit is contained in:
Ziver Koc 2018-08-08 11:34:44 +02:00
parent 0c8f71d80d
commit 5065c24f39
5 changed files with 106 additions and 101 deletions

View file

@ -81,8 +81,8 @@ public class Zallery extends HttpServlet{
} }
public static UserMessageManager getUserMessage(HttpSession session) { public static UserMessageManager getUserMessage(HttpSession session) {
if (session.getAttribute(ZalleryConstants.SESSION_KEY_USER_MSG) == null) if (session.getAttribute(ZalleryConstants.KEY_USER_MSG) == null)
session.setAttribute(ZalleryConstants.SESSION_KEY_USER_MSG, new UserMessageManager()); session.setAttribute(ZalleryConstants.KEY_USER_MSG, new UserMessageManager());
return (UserMessageManager) session.getAttribute(ZalleryConstants.SESSION_KEY_USER_MSG); return (UserMessageManager) session.getAttribute(ZalleryConstants.KEY_USER_MSG);
} }
} }

View file

@ -9,7 +9,7 @@ public interface ZalleryConstants {
/** Session Constants **/ /** Session Constants **/
public static final String SESSION_KEY_USER_MSG = "zall_user_message"; public static final String KEY_USER_MSG = "zall_user_message";
/** Language Key Constants **/ /** Language Key Constants **/

View file

@ -2,6 +2,7 @@ package zall;
import zutil.db.DBConnection; import zutil.db.DBConnection;
import zutil.log.LogUtil; import zutil.log.LogUtil;
import zutil.ui.UserMessageManager;
import javax.servlet.RequestDispatcher; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException; import javax.servlet.ServletException;
@ -22,7 +23,10 @@ public abstract class ZalleryServlet extends HttpServlet {
public final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException { public final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
DBConnection db = null; DBConnection db = null;
try { try {
UserMessageManager msgs = Zallery.getUserMessage(request.getSession());
request.setAttribute(ZalleryConstants.KEY_USER_MSG, msgs);
doGet(request, response, db = Zallery.getDB()); doGet(request, response, db = Zallery.getDB());
msgs.decrementViewCount();
} catch (ServletException e) { } catch (ServletException e) {
throw e; throw e;
} catch (Exception e) { } catch (Exception e) {

View file

@ -5,7 +5,6 @@ import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -141,10 +140,10 @@ public class Folder extends DBBean {
/** /**
* @param filename is the name of the file
* @param size specifies the size of the image
* @return a File object that points to the physical file on the disk, * @return a File object that points to the physical file on the disk,
* or null if the user or the filename is null * or null if the user or the filename is null
* @param filename is the name of the file
* @param size specifies the size of the image
*/ */
public File getFile(String filename, Image.Size size) { public File getFile(String filename, Image.Size size) {
// Zallery not initialized. // Zallery not initialized.

View file

@ -21,99 +21,101 @@ import zutil.image.ImageUtil;
import zutil.io.file.FileUtil; import zutil.io.file.FileUtil;
import zutil.log.LogUtil; import zutil.log.LogUtil;
@DBTable(value="Image", superBean=true) @DBTable(value = "Image", superBean = true)
public class Image extends Media{ public class Image extends Media {
private static final Logger logger = LogUtil.getLogger(); private static final Logger logger = LogUtil.getLogger();
public static final String type = "image"; public static final String type = "image";
public static final String IMAGE_FORMAT = "jpg"; public static final String IMAGE_FORMAT = "jpg";
@DBLinkTable(table="Comments", beanClass=Comment.class, idColumn="image")
private LinkedList<Comment> comments;
/**
* Loads all the Images from a folder
*/
public static List<Image> loadFolder(DBConnection db, Folder folder) throws SQLException{
if( folder == null || folder.getId() == null )
return new LinkedList<Image>();
PreparedStatement sql = db.getPreparedStatement("SELECT * FROM Image WHERE folder=? ORDER BY date DESC");
sql.setLong(1, folder.getId() );
return DBConnection.exec(sql, DBBeanSQLResultHandler.createList(Image.class, db));
}
public static Image load(DBConnection db, long id) throws SQLException{
return DBBean.load(db, Image.class, id);
}
public Image(){ @DBLinkTable(table = "Comments", beanClass = Comment.class, idColumn = "image")
super(); private LinkedList<Comment> comments;
comments = new LinkedList<Comment>();
} /**
* Loads all the Images from a folder
public LinkedList<Comment> getComments() { */
return comments; public static List<Image> loadFolder(DBConnection db, Folder folder) throws SQLException {
} if (folder == null || folder.getId() == null)
public void addComment(Comment cm){ return new LinkedList<Image>();
comments.add( cm ); PreparedStatement sql = db.getPreparedStatement("SELECT * FROM Image WHERE folder=? ORDER BY date DESC");
} sql.setLong(1, folder.getId());
return DBConnection.exec(sql, DBBeanSQLResultHandler.createList(Image.class, db));
public void setFile(FileItem item) throws Exception{ }
if( folder == null )
throw new Exception("Folder not set for image!"); public static Image load(DBConnection db, long id) throws SQLException {
// Generate unique filename return DBBean.load(db, Image.class, id);
filename = genFileName( item.getName() ); }
filename += "."+FileUtil.getFileExtension( item.getName() );
File file = folder.getFile( filename, Size.ORIGINAL );
public Image() {
// Move uploaded file super();
item.write( file ); comments = new LinkedList<Comment>();
} }
/** public LinkedList<Comment> getComments() {
* Returns the file for the image, and generates an thumbnail if there is no thumbnail return comments;
* }
* @param size is the size of the image
*/ public void addComment(Comment cm) {
public File getFile(Size size) throws IOException{ comments.add(cm);
if( filename != null ){ }
switch( size ){
case ORIGINAL: public void setFile(FileItem item) throws Exception {
return folder.getFile( filename, Size.ORIGINAL ); if (folder == null)
default: throw new Exception("Folder not set for image!");
File file = folder.getFile( FileUtil.replaceExtension(filename, IMAGE_FORMAT), size ); // Generate unique filename
File orgFile = folder.getFile( filename, Size.ORIGINAL ); filename = genFileName(item.getName());
if( !file.exists() ){ filename += "." + FileUtil.getFileExtension(item.getName());
if(orgFile.exists() && orgFile.canRead() ){ File file = folder.getFile(filename, Size.ORIGINAL);
// Generate new thumbnail
BufferedImage original = ImageIO.read( orgFile ); // Move uploaded file
BufferedImage image = null; item.write(file);
switch( size ){ }
case SMALL:
image = ImageUtil.cropScale(original, 125, 125); /**
break; * Returns the file for the image, and generates an thumbnail if there is no thumbnail
case MEDIUM: *
image = ImageUtil.scale(original, 500, 375, true); * @param size is the size of the image
break; */
case LARGE: public File getFile(Size size) throws IOException {
image = ImageUtil.scale(original, 1200, 800, true); if (filename != null) {
break; switch (size) {
default: case ORIGINAL:
break; return folder.getFile(filename, Size.ORIGINAL);
} default:
ImageIO.write(image, IMAGE_FORMAT, file); File file = folder.getFile(FileUtil.replaceExtension(filename, IMAGE_FORMAT), size);
} File orgFile = folder.getFile(filename, Size.ORIGINAL);
else if( !orgFile.exists() ) if (!file.exists()) {
logger.severe("Original image file missing: \""+file.getAbsolutePath()+"\""); if (orgFile.exists() && orgFile.canRead()) {
else if( orgFile.canRead() ) // Generate new thumbnail
logger.severe("Can not read original image file: \""+file.getAbsolutePath()+"\""); BufferedImage original = ImageIO.read(orgFile);
} BufferedImage image = null;
return file; switch (size) {
} case SMALL:
} image = ImageUtil.cropScale(original, 125, 125);
return null; break;
} case MEDIUM:
@Override image = ImageUtil.scale(original, 500, 375, true);
public String getType() { break;
return type; case LARGE:
} image = ImageUtil.scale(original, 1200, 800, true);
break;
default:
break;
}
ImageIO.write(image, IMAGE_FORMAT, file);
} else if (!orgFile.exists())
logger.severe("Original image file missing: \"" + file.getAbsolutePath() + "\"");
else if (orgFile.canRead())
logger.severe("Can not read original image file: \"" + file.getAbsolutePath() + "\"");
}
return file;
}
}
return null;
}
@Override
public String getType() {
return type;
}
} }