/* * 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.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletContext; import org.apache.tapestry.IAsset; import org.apache.tapestry.IRequestCycle; import org.apache.tapestry.asset.ContextAsset; import org.apache.tapestry.request.IUploadFile; import org.apache.tapestry.resource.ContextResourceLocation; import org.wamblee.photos.model.Album; import org.wamblee.photos.model.Photo; import org.wamblee.photos.model.PhotoEntry; import org.wamblee.photos.model.authorization.CreateAlbumOperation; import org.wamblee.photos.model.authorization.UploadPhotosOperation; /** * Album bean class for presenting an album. */ public class AlbumPage extends PhotoEntryBean { public static final String PAGE_NAME = "Home"; private static final String FOLDER_IMAGE = "folderImage"; /** * Maximum number of items displayed horizontally. */ private static final int NX = 5; /** * Maximum number of items displayed vertically. */ private static final int NY = 5; /** * Start index at which display starts. */ private int _startIndex; /** * Current row we are working on. */ private List _currentRow; /** * Current entry we are working on. */ private EntryRef _currentEntry; /** * Current page we are working on. */ private AlbumPageRef _currentPage; /** * Uploaded file. */ private IUploadFile _uploadedFile; /** * Optional message to display. */ private String _message; /** * Name of the folder to create. */ private String _newFolderName; /** * Constructs the page. * */ public AlbumPage() { super(); initializeImpl(); } public AlbumPage(Album aAlbum) { this(); setAlbumObject(aAlbum); initializeImpl(); } public void initialize() { super.initialize(); initializeImpl(); } /** * Initializes the page. */ private void initializeImpl() { _startIndex = 0; _currentRow = null; _currentEntry = null; _currentPage = null; _uploadedFile = null; _newFolderName = null; _message = ""; } public List getPages() { Album album = getAlbumObject(); int n = album.size(); List result = new ArrayList(); for (int page = 1; pageNoToIndex(page) < n; page++) { int index = pageNoToIndex(page); result.add(new AlbumPageRef(page, index, getRelativePath(), index != _startIndex)); } if (result.size() == 1) { result.clear(); // No sense to show page numbers if there is only // one page. } return result; } public void setCurrentPage(AlbumPageRef aCurrentPage) { _currentPage = aCurrentPage; } public AlbumPageRef getCurrentPage() { return _currentPage; } public AlbumPageRef getPrev() { if (_startIndex - NX * NY < 0) { return new AlbumPageRef(0, 0, null, false); } int page = _startIndex / NX / NY + 1; return new AlbumPageRef(page - 1, _startIndex - NX * NY, getRelativePath(), true); } public AlbumPageRef getNext() { if (_startIndex + NX * NY >= getAlbumObject().size()) { return new AlbumPageRef(0, 0, null, false); } int page = _startIndex / NX / NY + 1; return new AlbumPageRef(page + 1, _startIndex + NX * NY, getRelativePath(), true); } private int pageNoToIndex(int aPageNo) { return (aPageNo - 1) * NX * NY; } public void setAlbumObject(Album aAlbum) { super.setRelativePath(aAlbum.getPath()); } /** * @param aStartIndex * The startIndex to set. */ public void setStartIndex(int aStartIndex) { _startIndex = aStartIndex; } public Album getAlbumObject() { Album album = getPhotoAlbum(getRequestCycle()).getAlbum(getRelativePath()); if (album == null) { throw new IllegalArgumentException("album for '" + getRelativePath() + "' is null"); } return album; } public List getEntries() { Album album = getAlbumObject(); List result = new ArrayList(); int size = album.size(); for (int i = _startIndex; i < size && i < _startIndex + NX * NY; i++) { PhotoEntry entry = album.getEntry(i); result.add(new EntryRef(entry.getPath(), true, entry instanceof Photo)); } return result; } public List> getRows() { List> rows = new ArrayList>(); Album album = getAlbumObject(); for (int j = 0; j < NY; j++) { List row = new ArrayList(); for (int i = 0; i < NX; i++) { int index = _startIndex + i + j * NX; if (index < album.size()) { PhotoEntry entry = album.getEntry(index); row.add(new EntryRef(entry.getPath(), true, entry instanceof Photo)); } } if (row.size() > 0) { rows.add(row); } } return rows; } public void setCurrentRow(List aCurrentRow) { _currentRow = aCurrentRow; } public List getCurrentRow() { return _currentRow; } public void setCurrentEntry(EntryRef aCurrentEntry) { _currentEntry = aCurrentEntry; } public EntryRef getCurrentEntry() { return _currentEntry; } public int getStartIndex() { return _startIndex; } public void setSize(int aSize) { // No operation. } public IAsset getCurrentImage() { if (!_currentEntry.isPhoto()) { return getAsset(FOLDER_IMAGE); } ServletContext context = getPage().getRequestCycle() .getRequestContext().getServlet().getServletContext(); String url = IMAGE_URL_PREFIX + "/thumbnail" + _currentEntry.getRelativePath(); url = getRequestCycle().encodeURL(url); ContextResourceLocation resourceLocation = new ContextResourceLocation( context, "/" + url); return new ContextAsset(resourceLocation, null); } public void viewEntry(IRequestCycle aCycle) { Object[] params = aCycle.getServiceParameters(); String path = (String) params[0]; getVisitor().viewEntry(aCycle, path); } private Visit getVisitor() { return (Visit) getVisit(); } /** * @return Returns the file. */ public IUploadFile getFile() { return _uploadedFile; } /** * @param aFile * The file to set. */ public void setFile(IUploadFile aFile) { _uploadedFile = aFile; } /** * @return Returns the message. */ public String getMessage() { return _message; } /** * @param aMessage * The message to set. */ public void setMessage(String aMessage) { _message = aMessage; } public void uploadFile(IRequestCycle aCycle) throws IOException { IUploadFile file = getFile(); getVisitor().upload(aCycle, this, file); } public boolean isUploadAllowed() { return getPhotosEngine().getAuthorizationService().isAllowed(getAlbumObject(), new UploadPhotosOperation()); } /** * @return Returns the folder. */ public String getFolderName() { return _newFolderName; } /** * @param aNewFolderName * The folder to set. */ public void setFolderName(String aNewFolderName) { _newFolderName = aNewFolderName; } public boolean isAlbumCreationAllowed() { return getPhotosEngine().getAuthorizationService().isAllowed(getAlbumObject(), new CreateAlbumOperation()); } public void createFolder(IRequestCycle aCycle) throws IOException { getVisitor().createFolder(aCycle, _newFolderName); } }