Skip to content

Commit ef83764

Browse files
TimotheeHrlseboo
authored andcommitted
LUT-19213 : Add a draft status to blog post
1 parent af3469a commit ef83764

File tree

7 files changed

+89
-27
lines changed

7 files changed

+89
-27
lines changed

src/java/fr/paris/lutece/plugins/blog/business/BlogDAO.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import java.util.ArrayList;
3737
import java.util.List;
38+
import java.sql.Timestamp;
3839

3940
import org.apache.commons.lang3.BooleanUtils;
4041
import org.apache.commons.lang3.StringUtils;
@@ -78,7 +79,7 @@ public final class BlogDAO implements IBlogDAO
7879
private static final String SQL_QUERY_SELECT_BLOG_BY_ID_TAG = " SELECT b.id_blog, b.version, b.content_label, b.creation_date, b.update_date, b.html_content, b.user_editor, b.user_creator, b.attached_portlet_id, b.edit_comment, b.description, b.shareable, b.url, a.id_tag FROM blog_tag_document a Inner join blog_blog b on (b.id_blog = a.id_blog) WHERE a.id_tag = ? ORDER BY priority";
7980

8081
private static final String SQL_QUERY_SELECT_ALL_BLOG = " SELECT DISTINCT a.id_blog, a.version, a.content_label, a.creation_date, a.update_date, a.html_content, a.user_editor, a.user_creator , a.attached_portlet_id, a.edit_comment , a.description, a.shareable, a.url FROM blog_blog a";
81-
82+
private static final String SQL_QUERY_SELECT_VERSION_NUMBER_BY_BLOG_ID_AND_CREATION_DATE = "SELECT version FROM blog_versions WHERE id_blog = ? ORDER BY ABS(TIMESTAMPDIFF(SECOND, update_date, ?)) LIMIT 1;";
8283
private static final String SQL_FILTER_WHERE_CLAUSE = " WHERE ";
8384
private static final String SQL_FILTER_AND = " AND ";
8485
private static final String SQL_FILTER_TAGS_BEGIN = " (";
@@ -95,9 +96,9 @@ public final class BlogDAO implements IBlogDAO
9596

9697
/**
9798
* Generates a new primary key
98-
*
99+
*
99100
* @param plugin
100-
* The Plugin
101+
* The Plugin
101102
* @return The new primary key
102103
*/
103104
public int newPrimaryKey( Plugin plugin )
@@ -116,7 +117,7 @@ public int newPrimaryKey( Plugin plugin )
116117

117118
/**
118119
* Generates a new primary key
119-
*
120+
*
120121
* @param plugin
121122
* The Plugin
122123
* @return The new primary key
@@ -596,7 +597,7 @@ public List<Blog> selectByFilter( BlogFilter filter )
596597

597598
/**
598599
* Return a dao initialized with the specified filter
599-
*
600+
*
600601
* @param strQuerySelect
601602
* the query
602603
* @param filter
@@ -848,4 +849,31 @@ public List<Blog> selectLastBlogVersionsList( int nId, int nLimit, Plugin plugin
848849
}
849850
return blogList;
850851
}
852+
853+
/**
854+
* Returns the actual version number of a blog It get the version number witch has is the closest to the update date
855+
*
856+
* @param strUpdateDate
857+
* the update date
858+
* @param nId
859+
* the blog id
860+
* @return the version number
861+
*/
862+
@Override
863+
public int getActualVersionNumber( Timestamp strUpdateDate, int nId, Plugin plugin )
864+
{
865+
int nVersion = 0;
866+
try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_VERSION_NUMBER_BY_BLOG_ID_AND_CREATION_DATE, plugin ) )
867+
{
868+
daoUtil.setInt( 1, nId );
869+
daoUtil.setTimestamp( 2, strUpdateDate );
870+
daoUtil.executeQuery( );
871+
872+
if ( daoUtil.next( ) )
873+
{
874+
nVersion = daoUtil.getInt( 1 );
875+
}
876+
return nVersion;
877+
}
878+
}
851879
}

src/java/fr/paris/lutece/plugins/blog/business/BlogHome.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
package fr.paris.lutece.plugins.blog.business;
3535

3636
import java.util.List;
37-
37+
import java.sql.Timestamp;
3838
import fr.paris.lutece.portal.service.plugin.Plugin;
3939
import fr.paris.lutece.portal.service.plugin.PluginService;
4040
import fr.paris.lutece.portal.service.spring.SpringContextService;
@@ -263,7 +263,6 @@ public static Blog addInitialVersion( Blog blog )
263263

264264
return blog;
265265
}
266-
267266
/**
268267
* Adds a new version of an Blog
269268
*
@@ -273,7 +272,6 @@ public static Blog addInitialVersion( Blog blog )
273272
*/
274273
public static Blog addNewVersion( Blog blog )
275274
{
276-
BlogHome.update( blog );
277275
BlogHome.createVersion( blog );
278276

279277
return blog;
@@ -313,4 +311,16 @@ public static List<Blog> selectWithoutBinaries( )
313311
return _dao.selectWithoutBinaries( _plugin );
314312
}
315313

314+
/**
315+
* Returns the actual version number of a blog
316+
* It get the version number witch has is the closest to the update date
317+
* @param strUpdateDate the update date
318+
* @param nId the blog id
319+
* @return the version number
320+
*/
321+
public static int getActualVersionNumber( Timestamp strUpdateDate, int nId )
322+
{
323+
return _dao.getActualVersionNumber( strUpdateDate, nId, _plugin );
324+
}
325+
316326
}

src/java/fr/paris/lutece/plugins/blog/business/IBlogDAO.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,5 @@ public interface IBlogDAO
231231

232232
List<Blog> selectWithoutBinaries( Plugin plugin );
233233

234+
int getActualVersionNumber( java.sql.Timestamp strUpdateDate, int nId, fr.paris.lutece.portal.service.plugin.Plugin plugin );
234235
}

src/java/fr/paris/lutece/plugins/blog/resources/blog_messages.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ modify_blog.labelCreator.help=Author
149149
modify_blog.labelProperties=Properties
150150
modify_blog.labelAddToContent=Add to content
151151
modify_blog.labelToolbarLabel=Post management toolbar
152+
modify_blog.labelUpdate=Update
153+
modify_blog.labelSave=Save
154+
modify_blog.returnToActualVersion=Return to the current version
152155

153156
history_blog.pageTitle=History
154157
history_blog.title=History of this post

src/java/fr/paris/lutece/plugins/blog/resources/blog_messages_fr.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ modify_blog.labelLoading=Chargement...
150150
modify_blog.labelProperties=Propri\u00e9t\u00e9s
151151
modify_blog.labelAddToContent=Ajouter au contenu
152152
modify_blog.labelToolbarLabel=Boutons de gestion du billet
153+
modify_blog.labelUpdate=Actualiser
154+
modify_blog.labelSave=Enregistrer
155+
modify_blog.returnToActualVersion=Revenir \u00e0 la version actuelle
153156

154157
history_blog.pageTitle=Historique du billet
155158
history_blog.title=Liste des modification du billet

src/java/fr/paris/lutece/plugins/blog/web/BlogJspBean.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public class BlogJspBean extends ManageBlogJspBean
155155
protected static final String PARAMETER_PRIORITY = "tag_priority";
156156
protected static final String PARAMETER_TAG_ACTION = "tagAction";
157157
protected static final String PARAMETER_ACTION_BUTTON = "button";
158-
protected static final String PARAMETER_APPLY = "apply";
158+
protected static final String PARAMETER_UPDATE = "update";
159159
protected static final String PARAMETER_TYPE_ID = "idType";
160160
protected static final String PARAMETER_CONTENT_ID = "idContent";
161161
protected static final String PARAMETER_CONTENT_ACTION = "contentAction";
@@ -250,6 +250,7 @@ public class BlogJspBean extends ManageBlogJspBean
250250
protected static final String MARK_DATE_UPDATE_BLOG_BEFOR = "dateUpdateBlogBefor";
251251
protected static final String MARK_UNPUBLISHED = "unpublished";
252252
protected static final String MARK_LIST_BLOG_CONTRIBUTORS = "list_blog_contributors";
253+
protected static final String MARK_ACTUAL_BLOG_VERSION = "actual_blog_version";
253254

254255
public static final String CONSTANT_DUPLICATE_BLOG_NAME = "Copie de ";
255256

@@ -656,7 +657,7 @@ public String doCreateBlog( HttpServletRequest request )
656657
BlogService.getInstance( ).createBlog( _blog, _blog.getDocContent( ) );
657658
_blogServiceSession.removeDocContentFromSession( request.getSession( ), user );
658659

659-
if ( strAction != null && strAction.equals( PARAMETER_APPLY ) )
660+
if ( strAction != null && strAction.equals( PARAMETER_UPDATE ) )
660661
{
661662

662663
return redirect( request, VIEW_MODIFY_BLOG, PARAMETER_ID_BLOG, _blog.getId( ) );
@@ -952,22 +953,29 @@ public String getModifyBlog( HttpServletRequest request ) throws AccessDeniedExc
952953
String strResetVersion = request.getParameter( PARAMETER_VERSION_BLOG );
953954
String useCropImage = DatastoreService.getDataValue( PROPERTY_USE_UPLOAD_IMAGE_PLUGIN, "false" );
954955
String sessionId = request.getSession( ).getId( );
956+
Map<String, Object> model = getModel( );
955957

956958
int nVersion = -1;
957959
if ( strResetVersion != null )
958960
{
959961
nVersion = Integer.parseInt( strResetVersion );
960-
}
961-
962-
if ( strResetVersion != null )
963-
{
964-
965962
_blog = BlogHome.findVersion( nId, nVersion );
966963
_blogServiceSession.saveBlogInSession( request.getSession( ), _blog );
967964
}
968965
else
969966
{
970-
_blog = BlogService.getInstance( ).loadBlog( nId );
967+
Blog actualBlog = BlogService.getInstance( ).loadBlog( nId );
968+
Blog lastVersion = BlogHome.getLastBlogVersionsList( nId, 1 ).get( 0 );
969+
970+
// compare if content title, description or content are differents from the last version and actual version (the version that visitors could see if the blog is published)
971+
if ( !actualBlog.getContentLabel( ).equals( lastVersion.getContentLabel( ) ) || !actualBlog.getDescription( ).equals( lastVersion.getDescription( ) ) )
972+
{
973+
// get the actual version number that visitors could see so that the user can choose to reset to this version
974+
Timestamp actualVersionUpdateDate = actualBlog.getUpdateDate();
975+
int actualVersion = BlogHome.getActualVersionNumber( actualVersionUpdateDate,nId );
976+
model.put( MARK_ACTUAL_BLOG_VERSION, actualVersion );
977+
}
978+
_blog = lastVersion;
971979
_blogServiceSession.saveBlogInSession( request.getSession( ), _blog );
972980

973981
}
@@ -993,13 +1001,15 @@ public String getModifyBlog( HttpServletRequest request ) throws AccessDeniedExc
9931001

9941002
_blog.getTag( ).sort( ( tg1, tg2 ) -> tg1.getPriority( ) - tg2.getPriority( ) );
9951003
_blog.getDocContent( ).sort( ( dc1, dc2 ) -> dc1.getPriority( ) - dc2.getPriority( ) );
996-
Map<String, Object> model = getModel( );
9971004

9981005
boolean bPermissionCreate = RBACService.isAuthorized( Tag.PROPERTY_RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, Tag.PERMISSION_CREATE,
9991006
(User) getUser( ) );
1007+
boolean bPermissionToPublish = RBACService.isAuthorized( Blog.PROPERTY_RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, Blog.PERMISSION_PUBLISH,
1008+
(User) getUser( ) );
10001009

10011010
model.put( MARK_LIST_IMAGE_TYPE, DocContentHome.getListContentType( ) );
10021011
model.put( MARK_PERMISSION_CREATE_TAG, bPermissionCreate );
1012+
model.put( MARK_PERMISSION_PUBLISH_BLOG, bPermissionToPublish );
10031013
model.put( MARK_BLOG, _blog );
10041014
model.put( MARK_LIST_TAG, getTageList( ) );
10051015
model.put( MARK_USE_UPLOAD_IMAGE_PLUGIN, Boolean.parseBoolean( useCropImage ) );
@@ -1057,23 +1067,25 @@ public String doModifyBlog( HttpServletRequest request )
10571067
{
10581068
return redirect( request, VIEW_MODIFY_BLOG, PARAMETER_ID_BLOG, _blog.getId( ) );
10591069
}
1060-
1061-
if ( strAction != null && strAction.equals( PARAMETER_APPLY ) )
1070+
if ( strAction != null && strAction.equals( PARAMETER_UPDATE ) && RBACService.isAuthorized( Blog.PROPERTY_RESOURCE_TYPE, strId, Blog.PERMISSION_PUBLISH, (User) getUser( ) ) )
10621071
{
1063-
BlogService.getInstance( ).updateBlogWithoutVersion( _blog, _blog.getDocContent( ) );
1072+
_blog.setVersion( latestVersionBlog.getVersion( ) + 1 );
1073+
BlogHome.update( _blog );
1074+
BlogService.getInstance( ).updateBlog( _blog, _blog.getDocContent( ) );
10641075
_blogServiceSession.removeBlogFromSession( request.getSession( ), nId );
10651076
unLockBlog( nId );
1066-
1067-
return redirect( request, VIEW_MODIFY_BLOG, PARAMETER_ID_BLOG, _blog.getId( ) );
1068-
1077+
addInfo( INFO_BLOG_UPDATED, getLocale( ) );
10691078
}
10701079
else
10711080
{
10721081
_blog.setVersion( latestVersionBlog.getVersion( ) + 1 );
10731082
BlogService.getInstance( ).updateBlog( _blog, _blog.getDocContent( ) );
1083+
_blog = latestVersionBlog;
1084+
_blog.setVersion( latestVersionBlog.getVersion( ) + 1 );
1085+
BlogHome.update( _blog );
10741086
_blogServiceSession.removeBlogFromSession( request.getSession( ), nId );
10751087
unLockBlog( nId );
1076-
addInfo( INFO_BLOG_UPDATED, getLocale( ) );
1088+
return redirect( request, VIEW_MODIFY_BLOG, PARAMETER_ID_BLOG, _blog.getId( ) );
10771089
}
10781090
}
10791091
return redirectView( request, VIEW_MANAGE_BLOGS );

webapp/WEB-INF/templates/admin/plugins/blog/modify_blog.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
<@row id='blog-toolbar-wrapper'>
77
<@columns>
88
<@btnToolbar id='blog-toolbar' params='aria-label="#i18n{blog.modify_blog.labelToolbarLabel}"'>
9-
<@button class='me-1' type='submit' size='' buttonIcon='check me-2' title='#i18n{portal.util.labelModify}' value='modify' id='action_modifyblog' name='action_modifyblog' hideTitle=['xs','sm'] />
10-
<@button class='me-1' type='submit' size='' buttonIcon='device-floppy me-2' title='#i18n{blog.create_blog.labelApply}' value='apply' name='button' hideTitle=['xs','sm'] />
9+
<#if permission_manage_publish_blog?? && permission_manage_publish_blog>
10+
<@button class='me-1' type='submit' size='' buttonIcon='check me-2' title='#i18n{blog.modify_blog.labelUpdate}' value='update' name='button' hideTitle=['xs','sm'] />
11+
</#if>
12+
<@button class='me-1' type='submit' size='' buttonIcon='device-floppy me-2' title='#i18n{blog.modify_blog.labelSave}' value='modify' id='action_modifyblog' name='action_modifyblog' hideTitle=['xs','sm'] />
1113
<@aButton class='me-1' href='jsp/admin/plugins/blog/ManageBlogs.jsp?action=confirmRemoveBlog&amp;id=${blog.id}' color='danger' title='#i18n{portal.util.labelDelete}' buttonIcon='trash' hideTitle=['xs','sm'] size='' />
1214
<@aButton class='me-1' href='jsp/admin/plugins/blog/ManagePublicationBlogs.jsp?view=manageBlogsPublication&id=${blog.id}' title='#i18n{blog.publication_blog.pageTitle}' buttonIcon='globe' hideTitle=['xs','sm'] size='' />
1315
<@aButton href='jsp/admin/plugins/blog/ManageBlogs.jsp?view=historyBlog&amp;id=${blog.id}' class='btn-notif me-1' title='#i18n{blog.manage_blogs.labelHistory} [ ${blog.version} versions]' hideTitle=['xs','sm', 'md'] buttonIcon='history' size='' />
16+
<#if actual_blog_version?? >
17+
<@aButton class='btn-notif me-1' color='danger' href='jsp/admin/plugins/blog/ManageBlogs.jsp?view=modifyBlog&id=${blog.id}&blog_version=${actual_blog_version}' title='#i18n{blog.modify_blog.returnToActualVersion}' hideTitle=['xs','sm', 'md'] buttonIcon='history' size='' />
18+
</#if>
1419
<@offcanvas id='blog-properties' title='#i18n{blog.modify_blog.labelProperties}' btnTitle='#i18n{blog.modify_blog.labelProperties}' position='end' btnIcon='cog me-2'>
1520
<@box class='mt-3'>
1621
<@boxHeader title='#i18n{blog.modify_blog.TagsTitle}'>
@@ -140,4 +145,4 @@
140145
<@blogScriptInit />
141146
<script>
142147
<@charcounter />
143-
</script>
148+
</script>

0 commit comments

Comments
 (0)