bugfix: added missing setCaption() in fileasset.java

git-svn-id: https://svn.libreccm.org/ccm/trunk@3375 8810af33-2d31-482b-a856-94f89814c4df
master
konermann 2015-05-04 21:21:12 +00:00
parent 7a866b11e2
commit ed33bc8f77
1 changed files with 50 additions and 40 deletions

View File

@ -25,6 +25,7 @@ import com.arsdigita.persistence.DataObject;
import com.arsdigita.persistence.Filter; import com.arsdigita.persistence.Filter;
import com.arsdigita.persistence.OID; import com.arsdigita.persistence.OID;
import com.arsdigita.persistence.SessionManager; import com.arsdigita.persistence.SessionManager;
import com.arsdigita.util.StringUtils;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -33,19 +34,17 @@ import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Locale; import java.util.Locale;
/** /**
* An {@link com.arsdigita.cms.Asset asset} describing a concrete * An {@link com.arsdigita.cms.Asset asset} describing a concrete file, such as
* file, such as an image. * an image.
* *
* @author Scott Seago (scott@arsdigita.com) * @author Scott Seago (scott@arsdigita.com)
* @version $Id: FileAsset.java 2155 2011-01-21 01:17:02Z pboy $ * @version $Id: FileAsset.java 2155 2011-01-21 01:17:02Z pboy $
*/ */
public class FileAsset extends BinaryAsset { public class FileAsset extends BinaryAsset {
public static final String BASE_DATA_OBJECT_TYPE = public static final String BASE_DATA_OBJECT_TYPE
"com.arsdigita.cms.FileAsset"; = "com.arsdigita.cms.FileAsset";
public static final String CONTENT = "content"; public static final String CONTENT = "content";
public static final String HEIGHT = "height"; public static final String HEIGHT = "height";
@ -54,35 +53,35 @@ public class FileAsset extends BinaryAsset {
//public static final String MIME_JPEG = "image/jpeg"; //public static final String MIME_JPEG = "image/jpeg";
//public static final String MIME_GIF = "image/gif"; //public static final String MIME_GIF = "image/gif";
/** /**
* Default constructor. This creates a new text asset. * Default constructor. This creates a new text asset.
**/ *
*/
public FileAsset() { public FileAsset() {
super(BASE_DATA_OBJECT_TYPE); super(BASE_DATA_OBJECT_TYPE);
} }
/** /**
* Constructor. The contained <code>DataObject</code> is retrieved * Constructor. The contained <code>DataObject</code> is retrieved from the
* from the persistent storage mechanism with an <code>OID</code> * persistent storage mechanism with an <code>OID</code> specified by
* specified by <i>oid</i>. * <i>oid</i>.
* *
* @param oid The <code>OID</code> for the retrieved * @param oid The <code>OID</code> for the retrieved
* <code>DataObject</code>. * <code>DataObject</code>.
**/ *
*/
public FileAsset(OID oid) throws DataObjectNotFoundException { public FileAsset(OID oid) throws DataObjectNotFoundException {
super(oid); super(oid);
} }
/** /**
* Constructor. The contained <code>DataObject</code> is retrieved * Constructor. The contained <code>DataObject</code> is retrieved from the
* from the persistent storage mechanism with an <code>OID</code> * persistent storage mechanism with an <code>OID</code> specified by
* specified by <i>id</i> and * <i>id</i> and <code>FileAsset.BASE_DATA_OBJECT_TYPE</code>.
* <code>FileAsset.BASE_DATA_OBJECT_TYPE</code>.
* *
* @param id The <code>id</code> for the retrieved * @param id The <code>id</code> for the retrieved <code>DataObject</code>.
* <code>DataObject</code>. *
**/ */
public FileAsset(BigDecimal id) throws DataObjectNotFoundException { public FileAsset(BigDecimal id) throws DataObjectNotFoundException {
this(new OID(BASE_DATA_OBJECT_TYPE, id)); this(new OID(BASE_DATA_OBJECT_TYPE, id));
} }
@ -97,7 +96,7 @@ public class FileAsset extends BinaryAsset {
/** /**
* @return the base PDL object type for this item. Child classes should * @return the base PDL object type for this item. Child classes should
* override this method to return the correct value * override this method to return the correct value
*/ */
public String getBaseDataObjectType() { public String getBaseDataObjectType() {
return BASE_DATA_OBJECT_TYPE; return BASE_DATA_OBJECT_TYPE;
@ -152,20 +151,20 @@ public class FileAsset extends BinaryAsset {
} }
/** /**
* Load the file asset from the specified file. Automatically guesses * Load the file asset from the specified file. Automatically guesses the
* the mime type of the file. * mime type of the file.
* *
* @param fileName The original name of the file * @param fileName The original name of the file
* @param file The actual file on the server * @param file The actual file on the server
* @param defaultMimeType The default mime type for the file * @param defaultMimeType The default mime type for the file
*/ */
public void loadFromFile(String fileName, File file, String defaultMimeType) public void loadFromFile(String fileName, File file, String defaultMimeType)
throws IOException { throws IOException {
// Guess mime type // Guess mime type
MimeType mime = MimeType.guessMimeTypeFromFile(fileName); MimeType mime = MimeType.guessMimeTypeFromFile(fileName);
if(mime == null && defaultMimeType != null) { if (mime == null && defaultMimeType != null) {
// Set default mime type // Set default mime type
mime = MimeType.loadMimeType(defaultMimeType); mime = MimeType.loadMimeType(defaultMimeType);
} }
@ -174,12 +173,12 @@ public class FileAsset extends BinaryAsset {
// Extract the filename // Extract the filename
int i = fileName.lastIndexOf("/"); int i = fileName.lastIndexOf("/");
if(i > 0) { if (i > 0) {
fileName = fileName.substring(i+1); fileName = fileName.substring(i + 1);
} }
i = fileName.lastIndexOf("\\"); // DOS-style i = fileName.lastIndexOf("\\"); // DOS-style
if(i > 0) { if (i > 0) {
fileName = fileName.substring(i+1); fileName = fileName.substring(i + 1);
} }
setName(fileName); setName(fileName);
@ -188,13 +187,27 @@ public class FileAsset extends BinaryAsset {
readBytes(in); readBytes(in);
} }
/**
* creates a caption
*
* @author konerman
*/
public void setCaption() throws IOException {
MimeType mime;
mime = MimeType.loadMimeType("text/plain");
setMimeType(mime);
mime.setLabel("caption");
setName(StringUtils.urlize("caption"));
setContent("caption".getBytes());
}
/** /**
* Write the file asset content to a file. * Write the file asset content to a file.
* *
* @param file The file on the server to write to. * @param file The file on the server to write to.
*/ */
public void writeToFile(File file) public void writeToFile(File file)
throws IOException { throws IOException {
FileOutputStream fs = new FileOutputStream(file); FileOutputStream fs = new FileOutputStream(file);
try { try {
fs.write(getContent()); fs.write(getContent());
@ -206,15 +219,13 @@ public class FileAsset extends BinaryAsset {
} }
} }
/** /**
* Retrieve all files in the database. Extremely expensive ! * Retrieve all files in the database. Extremely expensive !
* *
* @return a collection of FileAssets * @return a collection of FileAssets
*/ */
public static FileAssetCollection getAllFiles() { public static FileAssetCollection getAllFiles() {
DataCollection da = SessionManager.getSession().retrieve DataCollection da = SessionManager.getSession().retrieve(BASE_DATA_OBJECT_TYPE);
(BASE_DATA_OBJECT_TYPE);
return new FileAssetCollection(da); return new FileAssetCollection(da);
} }
@ -223,12 +234,12 @@ public class FileAsset extends BinaryAsset {
* *
* @param keyword a String keyword * @param keyword a String keyword
* @param context the context for the retrieved items. Should be * @param context the context for the retrieved items. Should be
* {@link ContentItem#DRAFT} or {@link ContentItem#LIVE} * {@link ContentItem#DRAFT} or {@link ContentItem#LIVE}
* @return a collection of files whose name matches the keyword * @return a collection of files whose name matches the keyword
*/ */
public static FileAssetCollection getFilesByKeyword( public static FileAssetCollection getFilesByKeyword(
String keyword, String context String keyword, String context
) { ) {
FileAssetCollection c = getAllFiles(); FileAssetCollection c = getAllFiles();
c.addOrder(Asset.NAME); c.addOrder(Asset.NAME);
Filter f; Filter f;
@ -249,11 +260,10 @@ public class FileAsset extends BinaryAsset {
return getFilesByKeyword(keyword, ContentItem.DRAFT); return getFilesByKeyword(keyword, ContentItem.DRAFT);
} }
@Override @Override
protected void beforeSave() { protected void beforeSave() {
if( null == getLanguage() ) { if (null == getLanguage()) {
setLanguage( Locale.getDefault().getLanguage() ); setLanguage(Locale.getDefault().getLanguage());
} }
super.beforeSave(); super.beforeSave();