/* * Copyright 2005 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.wamblee.photos.tapestry; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import org.apache.tapestry.IRequestCycle; import org.apache.tapestry.request.IUploadFile; import org.wamblee.photos.model.Album; import org.wamblee.photos.model.Photo; import org.wamblee.photos.model.PhotoEntry; import org.wamblee.usermgt.Group; import org.wamblee.usermgt.User; import org.wamblee.usermgt.UserAdministration; import org.wamblee.usermgt.UserMgtException; /** * Visit object for the photos application. It provides the service interface * for pages. * */ public class Visit implements Serializable { /** * Extension to use for JPEGs. */ private static final String JPG_EXTENSION = ".jpg"; /** * Extension to use for ZIP files. */ private static final String ZIP_EXTENSION = ".zip"; /** * Uploads a file. * * @param aCycle * Request cycle. * @param aAlbum * Album to which the file must be uploaded. * @param aFile * File to upload. * @throws IOException * In case of problems. */ public void upload(IRequestCycle aCycle, AlbumPage aAlbum, IUploadFile aFile) throws IOException { String filename = new File(aFile.getFileName()).getName(); if (filename.trim().length() == 0) { return; } if (filename.toLowerCase().endsWith(JPG_EXTENSION)) { insertPhoto(aAlbum, aFile.getStream(), filename); } else if (filename.toLowerCase().endsWith(ZIP_EXTENSION)) { ZipInputStream zip = new ZipInputStream(aFile.getStream()); try { insertPhotosFromZipFile(aAlbum, zip); } finally { zip.close(); } } else { log(aAlbum, "Skipping entry with unknown file type '" + filename + "'"); } aCycle.activate(AlbumPage.PAGE_NAME); } /** * Insert photos from a zip file. * * @param aAlbum * Album to insert photos into. * @param aZipFile * Zip file containing photos. * @throws IOException * In case of problems. */ private void insertPhotosFromZipFile(AlbumPage aAlbum, ZipInputStream aZipFile) throws IOException { // zip extension ZipEntry entry; while ((entry = aZipFile.getNextEntry()) != null) { try { if (!entry.isDirectory() && entry.getName().toLowerCase() .endsWith(JPG_EXTENSION)) { insertPhoto(aAlbum, aZipFile, new File(entry.getName()).getName()); } else { log(aAlbum, "Skipping entry '" + entry.getName() + "' from zip file."); } } finally { aZipFile.closeEntry(); } } } /** * Insert a single photo. * * @param aAlbum * Album to insert into. * @param aPhotoInputStream * Input stream for the photo. * @param aFilename * Filename of the photo. * @throws IOException * In case of problems. */ private void insertPhoto(AlbumPage aAlbum, InputStream aPhotoInputStream, String aFilename) throws IOException { String photoName = aFilename.substring(0, aFilename.length() - JPG_EXTENSION.length()); Album album = aAlbum.getAlbumObject(); if (album.getEntry("/" + photoName) != null) { log(aAlbum, "Photo '" + photoName + "' already exists in this album"); return; } album.addImage(photoName, aPhotoInputStream); log(aAlbum, "Photo '" + photoName + "' uploaded"); } /** * Gets the photo album * * @param aCycle * Request cycle. * @return Photo album. */ private PhotoAlbum getPhotoAlbum(IRequestCycle aCycle) { return ((PhotosEngine) aCycle.getEngine()).getPhotoAlbum(aCycle); } /** * Forwards to a page to view a photo or album. * * @param aCycle * Request cycle. * @param aPath * Path of the photo or album. */ public void viewEntry(IRequestCycle aCycle, String aPath) { PhotoEntry entry = getPhotoAlbum(aCycle).getEntry(aPath); PhotoEntryBean page = (PhotoEntryBean) aCycle .getPage(entry instanceof Photo ? PhotoPage.PAGE_NAME : AlbumPage.PAGE_NAME); page.setRelativePath(aPath); aCycle.activate(page); } /** * Moves to another page of the current album. * * @param aCycle * Request cycle. * @param aNewPage * Page to move to. */ public void moveToPage(IRequestCycle aCycle, AlbumPageRef aNewPage) { AlbumPage page = (AlbumPage) aCycle.getPage(AlbumPage.PAGE_NAME); page.setStartIndex(aNewPage.getStartIndex()); page.setRelativePath(aNewPage.getRelativePath()); aCycle.activate(page); } /** * Creates a folder in the current album. * * @param aCycle * Request cycle. * @param aNewAlbum * Album name. * @throws IOException * In case of problems. */ public void createFolder(IRequestCycle aCycle, String aNewAlbum) throws IOException { AlbumPage page = (AlbumPage) aCycle.getPage(AlbumPage.PAGE_NAME); page.getAlbumObject().addAlbum(aNewAlbum); page.setMessage("Album '" + aNewAlbum + "' created"); aCycle.activate(page); } /** * Changes the current password for the currently logged in user. * * @param aCycle * Request cycle. * @param aOldPassword * Old password. * @param aNewPassword1 * New password. * @param aNewPassword2 * New password (second time). * @return Message to show. */ public String changePassword(IRequestCycle aCycle, String aOldPassword, String aNewPassword1, String aNewPassword2) { if (aOldPassword.length() == 0) { return "You must enter the current password"; } if (aNewPassword1.length() == 0) { return "You must enter a new password"; } if (aNewPassword1.equals(aNewPassword2)) { return changePassword(aCycle, aOldPassword, aNewPassword1); } else { return "New passwords are not equal"; } } /** * Changes the current password for the currently logged in user. * * @param aCycle * Request cycle. * @param aOldPassword * Old password. * @param aNewPassword * New password. * @return Message to display. */ public String changePassword(IRequestCycle aCycle, String aOldPassword, String aNewPassword) { User user = getPhotosEngine(aCycle).getUserAccessor().getCurrentUser(); try { user.changePassword(aOldPassword, aNewPassword); getPhotosEngine(aCycle).getUserAdministration().userModified(user); return "Password successfully changed"; } catch (UserMgtException e) { return e.getMessage(); } } /** * Changes the password for the currently logged in user. * * @param aCycle * Request cycle. * @param aNewPassword1 * New password. * @return Message to display. */ public String changePassword(IRequestCycle aCycle, String aNewPassword1) { User user = getPhotosEngine(aCycle).getUserAccessor().getCurrentUser(); try { user.setPassword(aNewPassword1); getPhotosEngine(aCycle).getUserAdministration().userModified(user); return "Password successfully changed"; } catch (UserMgtException e) { return e.getMessage(); } } public String changePasswordForUser(IRequestCycle aCycle, String aUser, String aPassword) { try { UserAdministration admin = getUserAdministration(aCycle); User user = admin.getUser(aUser); user.setPassword(aPassword); admin.userModified(user); return "Password changed for user '" + aUser + "'"; } catch (UserMgtException e) { return e.getMessage(); } } /** * Deletes the group. * * @param aCycle * Request cycle. * @param groupName * Group name. * @return Message to show. */ public String deleteGroup(IRequestCycle aCycle, String groupName) { try { Group group = getUserAdministration(aCycle).getGroup(groupName); getUserAdministration(aCycle).removeGroup(group); return "Group '" + groupName + "' deleted"; } catch (UserMgtException e) { return e.getMessage(); } } /** * Creates a group * * @param aCycle * Request cycle. * @param aGroup * Group. * @return Message to display. */ public String createGroup(IRequestCycle aCycle, String aGroup) { try { getUserAdministration(aCycle).createGroup(aGroup); return "Group '" + aGroup + "' created."; } catch (UserMgtException e) { return e.getMessage(); } } /** * Creates a user. * * @param aCycle * Request cycle. * @param aUser * User. * @param aPassword * Password * @param aGroup * Group * @return Message to display. */ public String createUser(IRequestCycle aCycle, String aUser, String aPassword, String aGroup) { try { UserAdministration admin = getUserAdministration(aCycle); Group group = admin.getGroup(aGroup); User user = admin.createUser(aUser, aPassword, group); return "User '" + aUser + "' created."; } catch (UserMgtException e) { return e.getMessage(); } } /** * Deletes a user. * * @param aCycle * Request cycle. * @param aUser * User. * @return Message to display. */ public String deleteUser(IRequestCycle aCycle, String aUser) { try { User user = getUserAdministration(aCycle).getUser(aUser); getUserAdministration(aCycle).removeUser(user); return "User '" + aUser + "' successfully created."; } catch (UserMgtException e) { return e.getMessage(); } } /** * Adds a user to a group. * * @param aCycle * Request cycle. * @param aUser * User. * @param aGroup * Group. * @return Message to display. */ public String addUserToGroup(IRequestCycle aCycle, String aUser, String aGroup) { try { UserAdministration admin = getUserAdministration(aCycle); Group group = admin.getGroup(aGroup); User user = admin.getUser(aUser); if (user.isInGroup(group)) { return "User '" + aUser + "' is already in group '" + aGroup + "'"; } admin.addUserToGroup(user, group); return "User '" + aUser + "' added to group '" + aGroup + "'"; } catch (UserMgtException e) { return e.getMessage(); } } /** * Removes a user from a group. * * @param aCycle * Request cycle. * @param aUser * User * @param aGroup * Group. * @return Message to display. */ public String removeUserFromGroup(IRequestCycle aCycle, String aUser, String aGroup) { try { UserAdministration admin = getUserAdministration(aCycle); Group group = admin.getGroup(aGroup); User user = admin.getUser(aUser); admin.removeUserFromGroup(user, group); return "User '" + aUser + "' removed from group '" + aGroup + "'"; } catch (UserMgtException e) { return e.getMessage(); } } /** * Logs a message and appends it to the current messages. * * @param aAlbum * Album. * @param aMessage * Message. */ private void log(AlbumPage aAlbum, String aMessage) { aAlbum.setMessage(aAlbum.getMessage() + "\n" + aMessage); } /** * Gets the engine. * * @param aCycle * Request cycle. * @return Engine object. */ private PhotosEngine getPhotosEngine(IRequestCycle aCycle) { return (PhotosEngine) aCycle.getEngine(); } /** * Gets the user administration. * * @param aCycle * Request cycle. * @return User administration. */ private UserAdministration getUserAdministration(IRequestCycle aCycle) { return getPhotosEngine(aCycle).getUserAdministration(); } }