Thumbnail Service: support for asyn creation of thumbnails (unit test's included)

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@9428 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Roy Wetherall
2008-06-09 10:10:16 +00:00
parent 997b5c0ab5
commit 5e60154bf5
7 changed files with 259 additions and 34 deletions

View File

@@ -0,0 +1,123 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.thumbnail;
import java.util.List;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.action.ParameterDefinitionImpl;
import org.alfresco.repo.action.executer.ActionExecuterAbstractBase;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ParameterDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.thumbnail.ThumbnailService;
import org.alfresco.service.namespace.QName;
/**
* Create thumbnail action executer.
*
* NOTE: This action is used to facilitate the async creation of thumbnails. It is not intended for genereral useage.
*
* @author Roy Wetherall
*/
public class CreateThumbnailActionExecuter extends ActionExecuterAbstractBase
{
/** Thumbnail Service */
private ThumbnailService thumbnailService;
/** Node Service */
private NodeService nodeService;
/** Action name and parameters */
public static final String NAME = "create-thumbnail";
public static final String PARAM_CONTENT_PROPERTY = "content-property";
public static final String PARAM_THUMBANIL_NAME = "thumbnail-name";
/**
* Set the thumbnail service
*
* @param thumbnailService the thumbnail service
*/
public void setThumbnailService(ThumbnailService thumbnailService)
{
this.thumbnailService = thumbnailService;
}
/**
* Set the node service
*
* @param nodeService node service
*/
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
/**
* @see org.alfresco.repo.action.executer.ActionExecuterAbstractBase#executeImpl(org.alfresco.service.cmr.action.Action, org.alfresco.service.cmr.repository.NodeRef)
*/
@Override
protected void executeImpl(Action action, NodeRef actionedUponNodeRef)
{
if (this.nodeService.exists(actionedUponNodeRef) == true)
{
// Get the thumbnail Name
String thumbnailName = (String)action.getParameterValue(PARAM_THUMBANIL_NAME);
// Get the details of the thumbnail
ThumbnailRegistry registry = this.thumbnailService.getThumbnailRegistry();
ThumbnailDetails details = registry.getThumbnailDetails(thumbnailName);
if (details == null)
{
// Throw exception
throw new AlfrescoRuntimeException("The thumbnail name '" + thumbnailName + "' is not registered");
}
// Get the content property
QName contentProperty = (QName)action.getParameterValue(PARAM_CONTENT_PROPERTY);
if (contentProperty == null)
{
contentProperty = ContentModel.PROP_CONTENT;
}
// Create the thumbnail
this.thumbnailService.createThumbnail(actionedUponNodeRef, contentProperty, details.getMimetype(), details.getTransformationOptions(), thumbnailName, null);
}
}
/**
* @see org.alfresco.repo.action.ParameterizedItemAbstractBase#addParameterDefinitions(java.util.List)
*/
@Override
protected void addParameterDefinitions(List<ParameterDefinition> paramList)
{
paramList.add(new ParameterDefinitionImpl(PARAM_THUMBANIL_NAME, DataTypeDefinition.TEXT, true, getParamDisplayLabel(PARAM_THUMBANIL_NAME)));
paramList.add(new ParameterDefinitionImpl(PARAM_CONTENT_PROPERTY, DataTypeDefinition.QNAME, false, getParamDisplayLabel(PARAM_CONTENT_PROPERTY)));
}
}

View File

@@ -77,29 +77,4 @@ public class ThumbnailRegistry
{
return this.thumbnailDetails.get(thumbnailName);
}
// /**
// *
// * @param node
// * @param contentProperty
// * @param thumbnailName
// * @return
// */
// public NodeRef createThumbnail(NodeRef node, QName contentProperty, String thumbnailName)
// {
// // Check to see if we have details of the thumbnail in our list
// ThumbnailDetails details = getThumbnailDetails(thumbnailName);
// if (details == null)
// {
// throw new ThumbnailException("The thumbnail name '" + thumbnailName + "' is not recognised");
// }
//
// // Create the thumbnail
// return this.thumbnailService.createThumbnail(
// node,
// contentProperty,
// details.getMimetype(),
// details.getTransformationOptions(),
// thumbnailName);
// }
}

View File

@@ -36,10 +36,13 @@ import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.content.transform.AbstractContentTransformerTest;
import org.alfresco.repo.content.transform.magick.ImageResizeOptions;
import org.alfresco.repo.content.transform.magick.ImageTransformationOptions;
import org.alfresco.repo.jscript.ClasspathScriptLocation;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.ScriptLocation;
import org.alfresco.service.cmr.repository.ScriptService;
import org.alfresco.service.cmr.repository.TransformationOptions;
import org.alfresco.service.cmr.thumbnail.ThumbnailException;
import org.alfresco.service.cmr.thumbnail.ThumbnailService;
@@ -56,6 +59,7 @@ import org.alfresco.util.BaseAlfrescoSpringTest;
public class ThumbnailServiceImplTest extends BaseAlfrescoSpringTest
{
private ThumbnailService thumbnailService;
private ScriptService scriptService;
private MimetypeMap mimetypeMap;
private NodeRef folder;
@@ -69,6 +73,7 @@ public class ThumbnailServiceImplTest extends BaseAlfrescoSpringTest
// Get the required services
this.thumbnailService = (ThumbnailService)this.applicationContext.getBean("ThumbnailService");
this.mimetypeMap = (MimetypeMap)this.applicationContext.getBean("mimetypeService");
this.scriptService = (ScriptService)this.applicationContext.getBean("ScriptService");
// Create a folder and some content
Map<QName, Serializable> folderProps = new HashMap<QName, Serializable>(1);
@@ -340,5 +345,20 @@ public class ThumbnailServiceImplTest extends BaseAlfrescoSpringTest
return node;
}
// == Test the JavaScript API ==
public void testJSAPI() throws Exception
{
NodeRef jpgOrig = createOrigionalContent(this.folder, MimetypeMap.MIMETYPE_IMAGE_JPEG);
NodeRef gifOrig = createOrigionalContent(this.folder, MimetypeMap.MIMETYPE_IMAGE_GIF);
Map<String, Object> model = new HashMap<String, Object>(2);
model.put("jpgOrig", jpgOrig);
model.put("gifOrig", gifOrig);
ScriptLocation location = new ClasspathScriptLocation("org/alfresco/repo/thumbnail/script/test_thumbnailAPI.js");
this.scriptService.executeScript(location, model);
}
}

View File

@@ -0,0 +1,31 @@
function testCreateThumbnail()
{
// Create a thumbnail
var thumbnail = jpgOrig.createThumbnail("medium");
test.assertNotNull(thumbnail);
// Create async thumbnail
// var thumbnail2 = gifOrig.createThumbnail("medium", true);
// test.assertNull(thumbnail2);
// Try and get the created thumbnail
// var count = 0;
// while (true)
// {
// thumbnail2 = gifOrig.getThumbnail("medium");
// if (thumbnail2 != null)
// {
// break;
// }
//else if (count > 1000)
//{
// test.fail("Async thumbanil wasn't created");
//}
// count++;
// }
}
// Execute the tests
testCreateThumbnail();