Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directory 274 #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@
<artifactId>library-image</artifactId>
<version>[1.0.0,)</version>
</dependency>
<!-- Blobstore -->
<dependency>
<groupId>fr.paris.lutece.plugins</groupId>
<artifactId>plugin-asynchronousupload</artifactId>
<version>1.0.5-SNAPSHOT</version>
<type>lutece-plugin</type>
</dependency>
<!-- Blobstore -->
<dependency>
<groupId>fr.paris.lutece.plugins</groupId>
<artifactId>library-blobstore</artifactId>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
*/
package fr.paris.lutece.plugins.directory.service.upload;

import fr.paris.lutece.plugins.asynchronousupload.service.AbstractAsynchronousUploadHandler;
import fr.paris.lutece.plugins.blobstore.service.BlobStoreClientException;
import fr.paris.lutece.plugins.blobstore.service.IBlobStoreClientService;
import fr.paris.lutece.plugins.directory.business.Entry;
import fr.paris.lutece.plugins.directory.business.EntryHome;
import fr.paris.lutece.plugins.directory.business.EntryTypeDownloadUrl;
import fr.paris.lutece.plugins.directory.business.File;
Expand All @@ -48,6 +50,7 @@
import fr.paris.lutece.plugins.directory.service.DirectoryPlugin;
import fr.paris.lutece.plugins.directory.utils.DirectoryErrorException;
import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
import static fr.paris.lutece.plugins.directory.utils.DirectoryUtils.getPlugin;
import fr.paris.lutece.plugins.directory.utils.JSONUtils;
import fr.paris.lutece.plugins.directory.utils.UrlUtils;
import fr.paris.lutece.portal.service.fileupload.FileUploadService;
Expand Down Expand Up @@ -81,6 +84,8 @@
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -95,7 +100,7 @@
* The uploaded files are deleted by SubForm when filling fields.
*
*/
public class DirectoryAsynchronousUploadHandler implements IAsynchronousUploadHandler
public class DirectoryAsynchronousUploadHandler extends AbstractAsynchronousUploadHandler
{
/** contains uploaded file items */
/**
Expand All @@ -104,22 +109,27 @@ public class DirectoryAsynchronousUploadHandler implements IAsynchronousUploadHa
public static Map<String, Map<String, List<FileItem>>> _mapAsynchronousUpload = new ConcurrentHashMap<String, Map<String, List<FileItem>>>( );
private static final String BEAN_DIRECTORY_ASYNCHRONOUS_UPLOAD_HANDLER = "directory.asynchronousUploadHandler";
private static final String PREFIX_ENTRY_ID = "directory_";


private static final String HANDLER_NAME = "DirectoryAsynchronousUploadHandler";

// UPLOAD
private static final String UPLOAD_SUBMIT_PREFIX = "_directory_upload_submit_directory_";
private static final String UPLOAD_DELETE_PREFIX = "_directory_upload_delete_directory_";
private static final String UPLOAD_CHECKBOX_PREFIX = "_directory_upload_checkbox_directory_";
private static final String UPLOAD_SUBMIT_PREFIX = "_upload_submit_directory_";
private static final String UPLOAD_DELETE_PREFIX = "_upload_delete_directory_";
private static final String UPLOAD_CHECKBOX_PREFIX = "_upload_checkbox_directory_";

// PARAMETERS
private static final String PARAMETER_BLOB_KEY = "blob_key";
private static final String PARAMETER_BLOBSTORE = "blobstore";
private static final String PARAMETER_PLUGIN_NAME = "plugin_name";
private static final String PARAMETER_FIELD_NAME = "field_name";
private static final String PARAMETER_FIELD_NAME = "fieldname";
private static final String PARAMETER_JSESSION_ID = "jsessionid";

// PROPERTIES
private static final String PROPERTY_MESSAGE_ERROR_UPLOADING_FILE_SESSION_LOST = "directory.message.error.uploading_file.session_lost";
private IBlobStoreClientService _blobStoreClientService;

//Error messages
private static final String ERROR_MESSAGE_UNKNOWN_ERROR = "directory.message.unknownError";

/**
* Private constructor
Expand Down Expand Up @@ -152,7 +162,8 @@ public void setBlobStoreClientService( IBlobStoreClientService blobStoreClientSe
@Override
public boolean isInvoked( HttpServletRequest request )
{
return DirectoryPlugin.PLUGIN_NAME.equals( request.getParameter( PARAMETER_PLUGIN_NAME ) );
return true;
//return DirectoryPlugin.PLUGIN_NAME.equals( request.getParameter( PARAMETER_PLUGIN_NAME ) );
}

/**
Expand All @@ -164,58 +175,7 @@ public boolean isBlobStoreClientServiceAvailable( )
return _blobStoreClientService != null;
}

/**
* {@inheritDoc}
* @category CALLED_BY_JS (directoryupload.js)
*/
@Override
public void process( HttpServletRequest request, HttpServletResponse response, JSONObject mainObject,
List<FileItem> listFileItemsToUpload )
{
// prevent 0 or multiple uploads for the same field
if ( ( listFileItemsToUpload == null ) || listFileItemsToUpload.isEmpty( ) )
{
throw new AppException( "No file uploaded" );
}

String strIdSession = request.getParameter( PARAMETER_JSESSION_ID );

if ( StringUtils.isNotBlank( strIdSession ) )
{
String strFieldName = request.getParameter( PARAMETER_FIELD_NAME );

if ( StringUtils.isBlank( strFieldName ) )
{
throw new AppException( "id entry is not provided for the current file upload" );
}

initMap( strIdSession, strFieldName );

// find session-related files in the map
Map<String, List<FileItem>> mapFileItemsSession = _mapAsynchronousUpload.get( strIdSession );

List<FileItem> fileItemsSession = mapFileItemsSession.get( strFieldName );

if ( canUploadFiles( strFieldName, fileItemsSession, listFileItemsToUpload, mainObject,
request.getLocale( ) ) )
{
fileItemsSession.addAll( listFileItemsToUpload );

JSONObject jsonListFileItems = JSONUtils.getUploadedFileJSON( fileItemsSession );
mainObject.accumulateAll( jsonListFileItems );
// add entry id to json
JSONUtils.buildJsonSuccess( strFieldName, mainObject );
}
}
else
{
AppLogService.error( DirectoryAsynchronousUploadHandler.class.getName( ) + " : Session does not exists" );

String strMessage = I18nService.getLocalizedString( PROPERTY_MESSAGE_ERROR_UPLOADING_FILE_SESSION_LOST,
request.getLocale( ) );
JSONUtils.buildJsonError( mainObject, strMessage );
}
}


/**
* Do upload a file in the blobstore webapp
Expand Down Expand Up @@ -484,7 +444,7 @@ public String getUploadAction( HttpServletRequest request )
{
String paramName = enumParamNames.nextElement( );

if ( paramName.startsWith( UPLOAD_SUBMIT_PREFIX ) || paramName.startsWith( UPLOAD_DELETE_PREFIX ) )
if ( paramName.startsWith( DirectoryAsynchronousUploadHandler.getHandler().getHandlerName()+UPLOAD_SUBMIT_PREFIX ) || paramName.startsWith( DirectoryAsynchronousUploadHandler.getHandler().getHandlerName()+UPLOAD_DELETE_PREFIX ) )
{
return paramName;
}
Expand Down Expand Up @@ -789,6 +749,118 @@ private void canUploadFiles( String strFieldName, List<FileItem> listUploadedFil
}
}

@Override
public String canUploadFiles(HttpServletRequest request, String strFieldName, List<FileItem> listFileItemsToUpload, Locale locale) {
if( StringUtils.isNotBlank( strFieldName )&&( strFieldName.length( ) > PREFIX_ENTRY_ID.length( ) ) )
{
String strIdEntry = strFieldName.substring( PREFIX_ENTRY_ID.length( ) );
int nIdEntry = DirectoryUtils.convertStringToInt( strIdEntry );
IEntry entry = EntryHome.findByPrimaryKey( nIdEntry, DirectoryUtils.getPlugin( ) );

List<FileItem> listUploadedFileItems = getListUploadedFiles( strFieldName, request.getSession( ) );

if ( entry != null )
{
try
{
entry.canUploadFiles( listUploadedFileItems, listFileItemsToUpload, locale );
}
catch ( DirectoryErrorException ex )
{
Logger.getLogger(DirectoryAsynchronousUploadHandler.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
return I18nService.getLocalizedString( ERROR_MESSAGE_UNKNOWN_ERROR, locale );
}

@Override
public List<FileItem> getListUploadedFiles(String strFieldName, HttpSession session) {
if ( StringUtils.isBlank( strFieldName ) )
{
throw new AppException( "id field name is not provided for the current file upload" );
}

initMap( session.getId( ), strFieldName );

// find session-related files in the map
Map<String, List<FileItem>> mapFileItemsSession = _mapAsynchronousUpload.get( session.getId( ) );

return mapFileItemsSession.get( strFieldName );
}

@Override
public void removeFileItem(String strFieldName, HttpSession session, int nIndex) {
// Remove the file (this will also delete the file physically)
List<FileItem> uploadedFiles = getListUploadedFiles( strFieldName, session );

if ( ( uploadedFiles != null ) && !uploadedFiles.isEmpty( ) && ( uploadedFiles.size( ) > nIndex ) )
{
// Remove the object from the Hashmap
FileItem fileItem = uploadedFiles.remove( nIndex );
fileItem.delete( );
}
}

@Override
public void addFileItemToUploadedFilesList(FileItem fileItem, String strFieldName, HttpServletRequest request) {
// This is the name that will be displayed in the form. We keep
// the original name, but clean it to make it cross-platform.
String strFileName = UploadUtil.cleanFileName( fileItem.getName( ).trim( ) );

initMap( request.getSession( ).getId( ), buildFieldName( strFieldName ) );

// Check if this file has not already been uploaded
List<FileItem> uploadedFiles = getListUploadedFiles( strFieldName, request.getSession( ) );

if ( uploadedFiles != null )
{
boolean bNew = true;

if ( !uploadedFiles.isEmpty( ) )
{
Iterator<FileItem> iterUploadedFiles = uploadedFiles.iterator( );

while ( bNew && iterUploadedFiles.hasNext( ) )
{
FileItem uploadedFile = iterUploadedFiles.next( );
String strUploadedFileName = UploadUtil.cleanFileName( uploadedFile.getName( ).trim( ) );
// If we find a file with the same name and the same
// length, we consider that the current file has
// already been uploaded
bNew = !( StringUtils.equals( strUploadedFileName, strFileName ) &&
( uploadedFile.getSize( ) == fileItem.getSize( ) ) );
}
}

if ( bNew )
{
uploadedFiles.add( fileItem );
}
}
}

@Override
public String getHandlerName() {
return HANDLER_NAME;
}

/**
* Get the id of the entry associated with a given field name
* @param strFieldName The name of the field
* @return The id of the entry
*/
protected String getEntryIdFromFieldName( String strFieldName )
{
if ( StringUtils.isEmpty( strFieldName ) || ( strFieldName.length( ) < PREFIX_ENTRY_ID.length( ) ) )
{
return null;
}

return strFieldName.substring( PREFIX_ENTRY_ID.length( ) );
}

/**
*
* DirectoryFileItem : builds fileItem from json response.
Expand Down Expand Up @@ -946,4 +1018,4 @@ public void write( java.io.File file ) throws Exception
// nothing
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ public final class DirectoryUtils

// JSP
public static final String JSP_MANAGE_DIRECTORY_RECORD = "jsp/admin/plugins/directory/ManageDirectoryRecord.jsp";
private static final String JSP_CREATE_DIRECTORY_RECORD = "jsp/admin/plugins/directory/CreateDirectoryRecord.jsp";
private static final String JSP_MODIFY_DIRECTORY_RECORD = "jsp/admin/plugins/directory/ModifyDirectoryRecord.jsp";

// SESSION
public static final String SESSION_DIRECTORY_LIST_SUBMITTED_RECORD_FIELDS = "directory_list_submitted_record_fields";
Expand All @@ -163,6 +165,7 @@ public final class DirectoryUtils
private static final String CONSTANTE_CHARACTER_RETURN = "\r";
private static final String REGEX_ID = "^[\\d]+$";


/**
* DirectoryUtils
*
Expand Down Expand Up @@ -1323,6 +1326,19 @@ public static String getJspManageDirectoryRecord( HttpServletRequest request, in

return urlItem.getUrl( );
}

/**
* return url of the jsp create directory record
* @param request the HTTP request
* @param nIdDirectory the directory id
* @return url of jsp create directory record
*/
public static String getJspModifyDirectoryRecord(HttpServletRequest request, int nIdDirectory, int nIdDirectoryRecord) {
UrlItem urlItem = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_MODIFY_DIRECTORY_RECORD );
urlItem.addParameter( PARAMETER_ID_DIRECTORY, nIdDirectory );
urlItem.addParameter( PARAMETER_ID_DIRECTORY_RECORD, nIdDirectoryRecord);
return urlItem.getUrl();
}

/**
* Convert a map of ( String, String ) into a {@link ReferenceList}
Expand Down
28 changes: 27 additions & 1 deletion src/java/fr/paris/lutece/plugins/directory/web/DirectoryApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ public class DirectoryApp implements XPageApplication
private static final String MARK_DIRECTORY = "directory";
private static final String MARK_STR_FORM_SEARCH = "str_form_search";
private static final String MARK_STR_RESULT_LIST = "str_result_list";
private static final String MARK_RESULT_RECORD_FIELD_LIST = "result_record_field_list";
private static final String MARK_WEBAPP_URL = "webapp_url";
private static final String MARK_STR_RESULT_RECORD = "str_result_record";
private static final String MARK_LOCALE = "locale";
private static final String MARK_PAGINATOR = "paginator";
Expand Down Expand Up @@ -342,6 +344,16 @@ else if ( listIdDirectory == null )

String strDirectoryRecord = getHtmlResultRecord( directory, record, request.getLocale( ), plugin,
session );

List<Integer> lIdRecordList = new ArrayList<Integer>();

lIdRecordList.add(record.getIdRecord());

if( lIdRecordList.size() > 0 )
{
List<RecordField> lRecordField = RecordFieldHome.getRecordFieldListByRecordIdList(lIdRecordList, plugin);
model.put( MARK_RESULT_RECORD_FIELD_LIST, lRecordField );
}
model.put( MARK_STR_RESULT_RECORD, strDirectoryRecord );
}
else
Expand Down Expand Up @@ -494,11 +506,24 @@ else if ( listIdDirectory == null )
model.put( MARK_PAGINATOR, paginator );

List<Record> lRecord = recordService.loadListByListId( paginator.getPageItems( ), plugin );

if ( lRecord.size( ) > 0 )
{
String strResultList = getHtmlResultList( directory, lRecord, request.getLocale( ),
plugin );

List<Integer> lIdRecordList = new ArrayList<Integer>();
for( Record elementlRecord : lRecord )
{
lIdRecordList.add(elementlRecord.getIdRecord());
}
if( lIdRecordList.size() > 0 )
{
List<RecordField> lRecordField = RecordFieldHome.getRecordFieldListByRecordIdList(lIdRecordList, plugin);
model.put( MARK_RESULT_RECORD_FIELD_LIST, lRecordField );
}
//getRecordFieldListByRecordIdList( List<Integer> lIdRecordList, Plugin plugin )

model.put( MARK_STR_RESULT_LIST, strResultList );
}
}
Expand Down Expand Up @@ -538,6 +563,7 @@ else if ( listIdDirectory == null )

List<IEntry> entriesGeolocationList = EntryHome.getEntryList( filterGeolocation, plugin );
model.put( MARK_ENTRY_LIST_GEOLOCATION, entriesGeolocationList );
model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );

HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_XPAGE_FRAME_DIRECTORY,
request.getLocale( ), model );
Expand Down
Loading