/* * Copyright 2004 Chris Nelson * * 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.trails.component; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import ognl.Ognl; import ognl.OgnlException; import org.apache.tapestry.IAsset; import org.apache.tapestry.annotations.ComponentClass; import org.apache.tapestry.annotations.InjectObject; import org.apache.tapestry.annotations.Parameter; import org.apache.tapestry.services.ExpressionEvaluator; import org.apache.tapestry.util.ComponentAddress; import org.trails.component.blob.BlobDownloadService; import org.trails.component.blob.ITrailsBlob; import org.trails.component.blob.TrailsBlobAsset; import org.trails.descriptor.BlobDescriptorExtension; import org.trails.descriptor.DescriptorService; import org.trails.descriptor.IPropertyDescriptor; import org.trails.persistence.PersistenceService; /** * Produces a table for the list of instances */ @ComponentClass(allowBody = true, allowInformalParameters = true) public abstract class ObjectTable extends ClassDescriptorComponent { public static final String LINK_COLUMN = "linkColumnValue"; @Parameter(required = false, defaultValue = "false", cache = true) public abstract boolean isShowCollections(); public abstract void setShowCollections(boolean ShowCollections); @InjectObject("spring:persistenceService") public abstract PersistenceService getPersistenceService(); @Parameter public abstract List getInstances(); public abstract void setInstances(List instances); public abstract Object getCurrentObject(); public abstract void setCurrentObject(Object currentObject); @Parameter(cache = false, defaultValue = "currentObject") public abstract Object getObject(); public abstract void setObject(Object object); @Parameter(cache = false) public abstract String getRowsClass(); public abstract void setRowsClass(String rowsClass); @Parameter(cache = false, defaultValue = "10") public abstract int getPageSize(); public abstract void setPageSize(int rowsClass); public ComponentAddress getLinkBlockAddress(IPropertyDescriptor descriptor) { if (getBlockAddress(descriptor) != null) { return getBlockAddress(descriptor); } else { return new ComponentAddress(getComponent(LINK_COLUMN)); } } @InjectObject("service:tapestry.ognl.ExpressionEvaluator") public abstract ExpressionEvaluator getEvaluator(); /** * @return */ public List getColumns() { ArrayList columns = new ArrayList(); for (Iterator iter = getPropertyDescriptors().iterator(); iter.hasNext();) { IPropertyDescriptor descriptor = (IPropertyDescriptor) iter.next(); if (displaying(descriptor)) { if (getLinkProperty().equals(descriptor.getName()) && (getClassDescriptor().isAllowSave() || getClassDescriptor().isAllowRemove())) { /** * Add a link for the edit page following these rules: a) * Use the identifier descriptor if is displayable ( * summary=true ). b) Use the first displayable property if * the identifier property is not displayable * (summary=false) */ columns.add(new TrailsTableColumn(descriptor, getEvaluator(), getLinkBlockAddress(descriptor))); } else { columns.add(new TrailsTableColumn(descriptor, getEvaluator(), getBlockAddress(descriptor))); } } } return columns; } /** * @param descriptor * @return */ private boolean displaying(IPropertyDescriptor descriptor) { if (descriptor.isHidden() || !descriptor.isSummary()) { return false; } else if ((descriptor.isCollection() && isShowCollections()) || (!descriptor.isCollection())) { return true; } else { return false; } } /** * @return */ public String getIdentifierProperty() { return this.getIdentifierPropertyDescriptor().getName(); } /** * Returns the name of the property to be used as link to the editor. If the * default Id property is not displayable then return the name of the first * displayable property. * * @return */ public String getLinkProperty() { IPropertyDescriptor propertyDescriptor = this.getIdentifierPropertyDescriptor(); if (!this.displaying(propertyDescriptor)) propertyDescriptor = this.getFirstDisplayableProperty(); return propertyDescriptor.getName(); } /** * Returns the first displayable property. * * @return */ protected IPropertyDescriptor getFirstDisplayableProperty() { for (Iterator iter = getPropertyDescriptors().iterator(); iter.hasNext();) { IPropertyDescriptor descriptor = (IPropertyDescriptor) iter.next(); if (displaying(descriptor)) { return descriptor; } } return null; // If we get here, that means we have no displayable // descriptors // TODO check if we should throw an exception instead } /** * @return */ protected IPropertyDescriptor getIdentifierPropertyDescriptor() { return (IPropertyDescriptor) getClassDescriptor().getIdentifierDescriptor(); } public ComponentAddress getBlockAddress(IPropertyDescriptor descriptor) { String blockName = descriptor.getName() + "ColumnValue"; if (getPage().getComponents().containsKey(blockName)) { return new ComponentAddress(getPage().getPageName(), blockName); } else return null; } public Object getSource() { return getInstances(); } @InjectObject("spring:descriptorService") public abstract DescriptorService getDescriptorService(); @InjectObject("service:trails.BlobService") public abstract BlobDownloadService getDownloadService(); public BlobDescriptorExtension getBlobDescriptorExtension() { return getPhotoColumnPropertyDescriptor().getExtension(BlobDescriptorExtension.class); } public IAsset getByteArrayAsset() { IPropertyDescriptor idDescriptor = getDescriptorService().getClassDescriptor(getPhotoColumnPropertyDescriptor().getBeanType()).getIdentifierDescriptor(); /** * get the ID */ String id = ""; try { if (Ognl.getValue(idDescriptor.getName(), getObject()) != null) id = Ognl.getValue(idDescriptor.getName(), getObject()).toString(); } catch (OgnlException e) { e.printStackTrace(); } /** * get the blob */ ITrailsBlob trailsBlob = null; try { if (Ognl.getValue(getPhotoColumnPropertyDescriptor().getName(), getObject()) != null) { trailsBlob = (ITrailsBlob) Ognl.getValue(getPhotoColumnPropertyDescriptor().getName(), getObject()); } } catch (OgnlException e) { e.printStackTrace(); } return new TrailsBlobAsset(getDownloadService(), getClassDescriptor().getType().getName(), id, getPhotoColumnPropertyDescriptor().getName(), trailsBlob.getContentType(), trailsBlob.getFileName()); } public IPropertyDescriptor getPhotoColumnPropertyDescriptor() { IPropertyDescriptor result = null; for (Iterator iter = getPropertyDescriptors().iterator(); iter.hasNext();) { IPropertyDescriptor descriptor = (IPropertyDescriptor) iter.next(); if (displaying(descriptor) && descriptor.getName().equalsIgnoreCase("photo")) { if (descriptor.supportsExtension(org.trails.descriptor.BlobDescriptorExtension.class.getName())) { result = descriptor; break; } } } return result; } }