View Javadoc
1   package fr.paris.lutece.plugins.blog.service;
2   
3   import fr.paris.lutece.portal.service.datastore.DatastoreService;
4   import fr.paris.lutece.portal.service.util.AppLogService;
5   
6   import org.apache.commons.lang3.StringUtils;
7   
8   import java.text.ParseException;
9   import java.text.SimpleDateFormat;
10  import java.util.Date;
11  
12  /**
13   * Blog parameter service.
14   */
15  public class BlogParameterService
16  {
17      private static BlogParameterServicegParameterService.html#BlogParameterService">BlogParameterService _singleton = new BlogParameterService( );
18  
19      public static final String DSKEY_DEFAULT_NUMBER_MANDATORY_TAGS = "blog.advanced_parameters.number_mandatory_tags";
20      public static final String DSKEY_DEFAULT_DATE_END_PUBLISHING = "blog.advanced_parameters.default_date_end_publishing";
21      public static final String DSKEY_DEFAULT_EDITOR = "blog.advanced_parameters.editor";
22      public static final String DSKEY_USE_UPLOAD_IMAGE_PLUGIN = "use_upload_image_plugin";
23      public static final String DSKEY_USE_CONTENT_TYPE = "blog.advanced_parameters.use_content_type";
24      public static final String DSKEY_ACCEPTED_FILE_TYPES = "blog.advanced_parameters.accepted_file_types";
25      public static final String MARK_DEFAULT_NUMBER_MANDATORY_TAGS = "number_mandatory_tags";
26      public static final String MARK_DEFAULT_DATE_END_PUBLISHING = "default_date_end_publishing";
27      public static final String MARK_DEFAULT_EDITOR = "default_editor";
28      public static final String MARK_USE_UPLOAD_IMAGE_PLUGIN = "use_upload_image_plugin";
29      public static final String MARK_USE_CONTENT_TYPE = "use_content_type";
30      public static final String MARK_ACCEPTED_FILE_TYPES = "accepted_file_types";
31      public static final String DSKEY_DEFAULT_EDITOR_BACK_OFFICE = "core.backOffice.defaultEditor";
32      
33      public static final String DEFAULT_ACCEPTED_FILE_TYPES = "image/*,.pdf,.xls,.doc,.docx,.xlsx,.odt,.ods,.ppt,.pptx,.odp";
34      
35      private static final String FIELD_DATEFORMAT = "yyyy-MM-dd";
36      private static final String DB_DATEFORMAT = "dd/MM/yyyy";
37  
38      /**
39       * Get the unique instance of the service
40       *
41       * @return The unique instance
42       */
43      public static BlogParameterService getInstance( )
44      {
45          return _singleton;
46      }
47  
48      /**
49       * Do modify the number of mandatory tags for a blog
50       *
51       * @param nbMandatoryTags
52       */
53      public void updateNumberMandatoryTags(String nbMandatoryTags)
54      {
55          int valueNbMandatoryTags = 0;
56          try
57          {
58              valueNbMandatoryTags = Integer.parseInt(nbMandatoryTags);
59          }
60          catch (NumberFormatException e)
61          {
62              AppLogService.error("Incorrect value for number mandatory tags", e);
63          }
64  
65          if(valueNbMandatoryTags<0)
66          {
67              valueNbMandatoryTags=0;
68          }
69  
70          DatastoreService.setDataValue( DSKEY_DEFAULT_NUMBER_MANDATORY_TAGS, Integer.toString(valueNbMandatoryTags));
71      }
72  
73      /**
74       * Get the number of mandatory tags for a blog
75       *
76       * @return numberMandatoryTags
77       */
78      public int getNumberMandatoryTags( )
79      {
80          try
81          {
82              return Integer.parseInt(DatastoreService.getDataValue( DSKEY_DEFAULT_NUMBER_MANDATORY_TAGS , "0" ));
83          }
84          catch (NumberFormatException e)
85          {
86              return 0;
87          }
88      }
89  
90  
91      /**
92       * Update default end publishing date from string iso date
93       *
94       * @param strValue date format 'yyyy-mm-dd'
95       */
96      public void updateDefaultDateEndPublishing(String strValue)
97      {
98          if (StringUtils.isNotBlank( strValue ))
99          {
100             try
101             {
102                 SimpleDateFormat formatField = new SimpleDateFormat(FIELD_DATEFORMAT);
103                 Date date = formatField.parse(strValue);
104 
105                 SimpleDateFormat format = new SimpleDateFormat(DB_DATEFORMAT);
106                 String strDbDate = format.format(date);
107 
108                 DatastoreService.setDataValue( DSKEY_DEFAULT_DATE_END_PUBLISHING, strDbDate );
109             }
110             catch (ParseException e)
111             {
112                 AppLogService.error("Incorrect value for default date end publishing update", e);
113             }
114         }
115     }
116     
117     /**
118      * Get String default editor
119      *
120      * @return default editor
121      */
122     public java.sql.Date getDefaultDateEndPublishing()
123     {
124         SimpleDateFormat format = new SimpleDateFormat(DB_DATEFORMAT);
125         try
126         {
127             return new java.sql.Date(format.parse(DatastoreService.getDataValue( DSKEY_DEFAULT_DATE_END_PUBLISHING , "" )).getTime());
128         }
129         catch (ParseException e)
130         {
131             return null;
132         }
133     }
134    
135     /**
136      * Update default editor
137      *
138      * @param strValue editor name
139      */
140     public void updateDefaultEditor(String strValue)
141     {
142         try
143         {
144             DatastoreService.setDataValue( DSKEY_DEFAULT_EDITOR, strValue );
145         }
146         catch (Exception e)
147         {
148             AppLogService.error("Error updating editor config", e );
149         }
150     }
151 
152      /**
153      * Get String default editor name
154      *
155      * @return default editor name for plugin
156      */
157     public String getDefaultBlogEditor()
158     {
159         String strDefaultEditor = DatastoreService.getDataValue( DSKEY_DEFAULT_EDITOR_BACK_OFFICE, "" );
160         String strBlogDefaultEditor = DatastoreService.getDataValue( DSKEY_DEFAULT_EDITOR, "" );
161         if (StringUtils.isNotBlank( strBlogDefaultEditor ))
162         {
163             return strBlogDefaultEditor;
164         }
165         else
166         {
167             return strDefaultEditor;
168         }
169              
170     }
171 
172     /**
173      * Update the use upload image plugin setting
174      *
175      * @param bUseUploadImagePlugin true to enable, false to disable
176      */
177     public void updateUseUploadImagePlugin( boolean bUseUploadImagePlugin )
178     {
179         DatastoreService.setDataValue( DSKEY_USE_UPLOAD_IMAGE_PLUGIN, Boolean.toString( bUseUploadImagePlugin ) );
180     }
181 
182     /**
183      * Check if upload image plugin is enabled
184      *
185      * @return true if enabled, false otherwise
186      */
187     public boolean isUseUploadImagePlugin()
188     {
189         return Boolean.parseBoolean( DatastoreService.getDataValue( DSKEY_USE_UPLOAD_IMAGE_PLUGIN, "false" ) );
190     }
191 
192     /**
193      * Update the use content type setting
194      *
195      * @param bUseContentType true to enable, false to disable
196      */
197     public void updateUseContentType( boolean bUseContentType )
198     {
199         DatastoreService.setDataValue( DSKEY_USE_CONTENT_TYPE, Boolean.toString( bUseContentType ) );
200     }
201 
202     /**
203      * Check if content type is enabled
204      *
205      * @return true if enabled, false otherwise
206      */
207     public boolean isUseContentType()
208     {
209         return Boolean.parseBoolean( DatastoreService.getDataValue( DSKEY_USE_CONTENT_TYPE, "true" ) );
210     }
211 
212     /**
213      * Update the accepted file types for upload
214      *
215      * @param strAcceptedFileTypes the accepted file types (e.g., "image/*,.pdf,.doc")
216      */
217     public void updateAcceptedFileTypes( String strAcceptedFileTypes )
218     {
219         if ( StringUtils.isNotBlank( strAcceptedFileTypes ) )
220         {
221             DatastoreService.setDataValue( DSKEY_ACCEPTED_FILE_TYPES, strAcceptedFileTypes.trim() );
222         }
223         else
224         {
225             DatastoreService.setDataValue( DSKEY_ACCEPTED_FILE_TYPES, DEFAULT_ACCEPTED_FILE_TYPES );
226         }
227     }
228 
229     /**
230      * Get the accepted file types for upload
231      *
232      * @return the accepted file types string
233      */
234     public String getAcceptedFileTypes()
235     {
236         return DatastoreService.getDataValue( DSKEY_ACCEPTED_FILE_TYPES, DEFAULT_ACCEPTED_FILE_TYPES );
237     }
238 
239 }