View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.seo.modules.suggest.service;
35  
36  import java.text.MessageFormat;
37  import java.util.List;
38  
39  import fr.paris.lutece.plugins.suggest.business.Suggest;
40  import fr.paris.lutece.plugins.suggest.business.SuggestFilter;
41  import fr.paris.lutece.plugins.suggest.business.SuggestHome;
42  import fr.paris.lutece.plugins.suggest.service.SuggestPlugin;
43  import fr.paris.lutece.plugins.seo.business.FriendlyUrl;
44  import fr.paris.lutece.plugins.seo.service.FriendlyUrlUtils;
45  import fr.paris.lutece.plugins.seo.service.SEODataKeys;
46  import fr.paris.lutece.plugins.seo.service.generator.FriendlyUrlGenerator;
47  import fr.paris.lutece.plugins.seo.service.generator.GeneratorOptions;
48  import fr.paris.lutece.plugins.seo.service.sitemap.SitemapUtils;
49  import fr.paris.lutece.portal.service.datastore.DatastoreService;
50  import fr.paris.lutece.portal.service.plugin.PluginService;
51  
52  
53  /**
54   * Document Alias Generator
55   */
56  public class SuggestFriendlyUrlGenerator implements FriendlyUrlGenerator
57  {
58      private static final String GENERATOR_NAME = "Suggestlike Friendly URL Generator";
59      private static final String TECHNICAL_URL = "/jsp/site/Portal.jsp?page=suggest&id_suggest={0}";
60      private static final String SLASH = "/";
61      private static final String PATH_SUGGEST = "/suggest/";
62      private static final String DEFAULT_CHANGE_FREQ = SitemapUtils.CHANGE_FREQ_VALUES[3];
63      private static final String DEFAULT_PRIORITY = SitemapUtils.PRIORITY_VALUES[3];
64      private boolean _bCanonical;
65      private boolean _bSitemap;
66      private String _strChangeFreq;
67      private String _strPriority;
68  
69      /**
70       * {@inheritDoc }
71       */
72      @Override
73      public String generate( List<FriendlyUrl> list, GeneratorOptions options )
74      {
75          SuggestFilter filter = new SuggestFilter(  );
76          filter.setIdState( Suggest.STATE_ENABLE );
77          
78          List<Suggest> listSuggest = SuggestHome.getSuggestList( filter, PluginService.getPlugin( SuggestPlugin.PLUGIN_NAME )  );
79       
80        
81          init(  );
82  
83          for ( Suggest  suggest : listSuggest )
84          {
85             
86              FriendlyUrl url = new FriendlyUrl(  );
87  
88              if ( options.isAddPath(  ) )
89              {
90              	 String strPath = PATH_SUGGEST;
91                   url.setFriendlyUrl( strPath + FriendlyUrlUtils.convertToFriendlyUrl( suggest.getTitle(  )) );
92              }
93              else
94              {
95              	
96                  url.setFriendlyUrl( SLASH + FriendlyUrlUtils.convertToFriendlyUrl( suggest.getTitle(  ) ) );
97              }
98  
99              Object[] args = { suggest.getIdSuggest() };
100             String strTechnicalUrl = MessageFormat.format( TECHNICAL_URL, args );
101             url.setTechnicalUrl( strTechnicalUrl );
102             url.setCanonical( _bCanonical );
103             url.setSitemap( _bSitemap );
104             url.setSitemapChangeFreq( _strChangeFreq );
105             url.setSitemapLastmod( SitemapUtils.formatDate( suggest.getDateCreation() ) );
106             url.setSitemapPriority( _strPriority );
107             list.add( url );
108         }
109 
110         return "";
111     }
112 
113     /**
114      * {@inheritDoc }
115      */
116     @Override
117     public String getName(  )
118     {
119         return GENERATOR_NAME;
120     }
121 
122     /**
123      * Initialiaze generator with options stored with Datastore
124      */
125     private void init(  )
126     {
127         String strKeyPrefix = SEODataKeys.PREFIX_GENERATOR + getClass(  ).getName(  );
128         _bCanonical = DatastoreService.getDataValue( strKeyPrefix + SEODataKeys.SUFFIX_CANONICAL,
129                 DatastoreService.VALUE_TRUE ).equals( DatastoreService.VALUE_TRUE );
130         _bSitemap = DatastoreService.getDataValue( strKeyPrefix + SEODataKeys.SUFFIX_SITEMAP,
131                 DatastoreService.VALUE_TRUE ).equals( DatastoreService.VALUE_TRUE );
132         _strChangeFreq = DatastoreService.getDataValue( strKeyPrefix + SEODataKeys.SUFFIX_CHANGE_FREQ,
133                 DEFAULT_CHANGE_FREQ );
134         _strPriority = DatastoreService.getDataValue( strKeyPrefix + SEODataKeys.SUFFIX_PRIORITY, DEFAULT_PRIORITY );
135     }
136 }