View Javadoc
1   package fr.paris.lutece.plugins.notifygru.modules.extendcomment.service;
2   
3   import java.text.SimpleDateFormat;
4   import java.util.ArrayList;
5   import java.util.Collection;
6   
7   import javax.inject.Inject;
8   import javax.inject.Named;
9   
10  import fr.paris.lutece.plugins.extend.modules.comment.business.Comment;
11  import fr.paris.lutece.plugins.extend.modules.comment.service.CommentService;
12  import fr.paris.lutece.plugins.extend.modules.comment.service.ICommentService;
13  import fr.paris.lutece.plugins.extend.modules.comment.service.extender.CommentResourceExtender;
14  import fr.paris.lutece.plugins.extend.modules.comment.util.constants.CommentConstants;
15  import fr.paris.lutece.plugins.extend.service.extender.config.IResourceExtenderConfigService;
16  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
17  import fr.paris.lutece.plugins.workflowcore.service.provider.IProvider;
18  import fr.paris.lutece.plugins.workflowcore.service.provider.InfoMarker;
19  import fr.paris.lutece.portal.service.i18n.I18nService;
20  import fr.paris.lutece.portal.service.spring.SpringContextService;
21  import fr.paris.lutece.portal.service.util.AppException;
22  import fr.paris.lutece.portal.service.util.AppPropertiesService;
23  
24  public class ExtendCommentProvider implements IProvider {
25  
26  	// PROPERTY KEY
27      private static final String PROPERTY_SMS_SENDER_NAME = AppPropertiesService.getProperty( "notifygru-extendcomment.gruprovider.sms.sendername");
28      private static final String PROPERTIE_DATE_FORMAT = AppPropertiesService.getProperty( "notifygru-extendcomment.dateformat", "dd-MM-yyyy" );
29  	public static final String PROPERTY_ID_DEMAND_TYPE_CRM =AppPropertiesService.getProperty( "notifygru-extendcomment.crmDemandTypeId");
30      // SERVICES
31      private ICommentService _commentService = SpringContextService.getBean( CommentService.BEAN_SERVICE );
32  	@Inject
33  	@Named( CommentConstants.BEAN_CONFIG_SERVICE )
34  	private IResourceExtenderConfigService _configService;
35  	 
36       private final Comment _comment;
37  
38  	
39      public ExtendCommentProvider( String beanProviderName, String strProviderId, ResourceHistory resourceHistory)
40      {
41      	_comment = _commentService.findByPrimaryKey(resourceHistory.getIdResource( ));
42      	
43      	  if ( _comment == null )
44            {
45                throw new AppException( "No COMMENT for resource history Id : " + resourceHistory.getIdResource( ) );
46            }
47      }
48  	
49  	@Override
50  	public String provideCustomerConnectionId() {
51  		return _comment.getLuteceUserName();
52  
53  	}
54  
55  	@Override
56  	public String provideCustomerEmail() {
57  		
58  		return _comment.getEmail();
59  	}
60  
61  	@Override
62  	public String provideCustomerId() {
63  		
64  		return _comment.getLuteceUserName();
65  	}
66  
67  	@Override
68  	public String provideCustomerMobilePhone() {
69  		return null;
70  	}
71  
72  	@Override
73  	public String provideDemandId() {
74  		
75  		return String.valueOf( _comment.getIdComment( ));
76  	}
77  
78  	@Override
79  	public String provideDemandReference() {
80  		
81  		return CommentResourceExtender.EXTENDER_TYPE_COMMENT+String.valueOf(_comment.getIdComment());
82  	}
83  
84  	@Override
85  	public String provideDemandSubtypeId() {
86  		return null;
87  	}
88  
89  	@Override
90  	public String provideDemandTypeId() {
91  		
92  		 
93  		return   PROPERTY_ID_DEMAND_TYPE_CRM ;
94  	}
95  
96  	@Override
97  	public Collection<InfoMarker> provideMarkerValues() {
98  		
99  		
100 		Collection<InfoMarker> collectionNotifyGruMarkers = new ArrayList<>( );
101 		SimpleDateFormat formatter = new SimpleDateFormat( PROPERTIE_DATE_FORMAT );
102 		
103 		collectionNotifyGruMarkers.add( createMarkerValues( ExtendCommentConstants.MARK_COMMENT, _comment.getComment() ) );
104 		collectionNotifyGruMarkers.add( createMarkerValues( ExtendCommentConstants.MARK_DATE_COMMENT, formatter.format(_comment.getDateComment( ) )) );
105 		collectionNotifyGruMarkers.add( createMarkerValues( ExtendCommentConstants.MARK_DATE_LAST_MODIFICATION,formatter.format( _comment.getDateLastModif() ) ));
106 		collectionNotifyGruMarkers.add( createMarkerValues( ExtendCommentConstants.MARK_EXTENDABLE_RESOURCE_TYPE, _comment.getExtendableResourceType() ) );
107 		collectionNotifyGruMarkers.add( createMarkerValues( ExtendCommentConstants.MARK_ID_EXTENDABLE_RESOURCE, _comment.getIdExtendableResource( ) ) );
108 		collectionNotifyGruMarkers.add( createMarkerValues( ExtendCommentConstants.MARK_IS_ADMIN_COMMENT,String.valueOf( _comment.getIsAdminComment() )) );
109 		collectionNotifyGruMarkers.add( createMarkerValues( ExtendCommentConstants.MARK_IS_IMPORTANT, String.valueOf(_comment.getIsImportant() )) );
110 		collectionNotifyGruMarkers.add( createMarkerValues( ExtendCommentConstants.MARK_LUTECE_USER_NAME, _comment.getLuteceUserName( ) ) );
111 		collectionNotifyGruMarkers.add( createMarkerValues( ExtendCommentConstants.MARK_NAME, _comment.getName() ) );
112 		collectionNotifyGruMarkers.add( createMarkerValues( ExtendCommentConstants.MARK_PINNED, String.valueOf(_comment.isPinned() )) );
113 		collectionNotifyGruMarkers.add( createMarkerValues( ExtendCommentConstants.MARK_IS_PUBLISHED, String.valueOf(_comment.isPublished() ) ));
114 		
115 		return collectionNotifyGruMarkers;
116 	}
117 
118 	@Override
119 	public String provideSmsSender() {
120 		return PROPERTY_SMS_SENDER_NAME ;
121 	}
122 	
123 	/**
124      * static method for retrieving descriptions of available marks for a given comment
125      * 
126      * @param nComentId
127      *            id of the nCommentExtender
128      * @return Collection of InfoMarker
129      */
130     public static Collection<InfoMarker> getProviderMarkerDescriptions( int nCommentExtender )
131     {
132     	Collection<InfoMarker> collectionNotifyGruMarkers = new ArrayList<>( );
133 		
134 		collectionNotifyGruMarkers.add( createMarkerDescriptions( ExtendCommentConstants.MARK_COMMENT,ExtendCommentConstants.MESSAGE_MARK_COMMENT ,null ) );
135 		collectionNotifyGruMarkers.add( createMarkerDescriptions( ExtendCommentConstants.MARK_DATE_COMMENT,ExtendCommentConstants.MESSAGE_MARK_DATE_COMMENT ,null ) );
136 		collectionNotifyGruMarkers.add( createMarkerDescriptions( ExtendCommentConstants.MARK_DATE_LAST_MODIFICATION,ExtendCommentConstants.MESSAGE_MARK_DATE_LAST_MODIFICATION,null  ));
137 		collectionNotifyGruMarkers.add( createMarkerDescriptions( ExtendCommentConstants.MARK_EXTENDABLE_RESOURCE_TYPE,ExtendCommentConstants.MESSAGE_MARK_EXTENDABLE_RESOURCE_TYPE ,null ) );
138 		collectionNotifyGruMarkers.add( createMarkerDescriptions( ExtendCommentConstants.MARK_ID_EXTENDABLE_RESOURCE,ExtendCommentConstants.MESSAGE_MARK_ID_EXTENDABLE_RESOURCE ,null ) );
139 		collectionNotifyGruMarkers.add( createMarkerDescriptions( ExtendCommentConstants.MARK_IS_ADMIN_COMMENT,ExtendCommentConstants.MESSAGE_MARK_IS_ADMIN_COMMENT,null ) );
140 		collectionNotifyGruMarkers.add( createMarkerDescriptions( ExtendCommentConstants.MARK_IS_IMPORTANT, ExtendCommentConstants.MESSAGE_MARK_IS_IMPORTANT,null ) );
141 		collectionNotifyGruMarkers.add( createMarkerDescriptions( ExtendCommentConstants.MARK_LUTECE_USER_NAME,ExtendCommentConstants.MESSAGE_MARK_LUTECE_USER_NAME ,null ) );
142 		collectionNotifyGruMarkers.add( createMarkerDescriptions( ExtendCommentConstants.MARK_NAME, ExtendCommentConstants.MESSAGE_MARK_NAME,null  ) );
143 		collectionNotifyGruMarkers.add( createMarkerDescriptions( ExtendCommentConstants.MARK_PINNED, ExtendCommentConstants.MESSAGE_MARK_PINNED,null ) );
144 		collectionNotifyGruMarkers.add( createMarkerDescriptions( ExtendCommentConstants.MARK_IS_PUBLISHED,ExtendCommentConstants.MESSAGE_MARK_IS_PUBLISHED ,null  ));
145 		
146 		return collectionNotifyGruMarkers;
147     }
148 
149 
150     /**
151      * Construct a InfoMarker with value for given parameters
152      * 
153      * @param strMarker
154      * @param strValue
155      * @return a InfoMarker
156      */
157     private static InfoMarker createMarkerValues( String strMarker, String strValue )
158     {
159         InfoMarker notifyMarker = new InfoMarker( strMarker );
160         notifyMarker.setValue( strValue );
161 
162         return notifyMarker;
163     }
164     /**
165     * Construct a InfoMarker with descrition for given parameters
166     * 
167     * @param strMarker
168     * @param strDescription
169     * @return a InfoMarker
170     */
171    private static InfoMarker createMarkerDescriptions( String strMarker, String strDescriptionI18n, String strDescription )
172    {
173        InfoMarker notifyMarker = new InfoMarker( strMarker );
174        if ( strDescriptionI18n != null )
175        {
176            notifyMarker.setDescription( I18nService.getLocalizedString( strDescriptionI18n, I18nService.getDefaultLocale( ) ) );
177        }
178        else
179        {
180            notifyMarker.setDescription( strDescription );
181        }
182 
183        return notifyMarker;
184 }
185 }