View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.tagcloud.business.portlet;
35  
36  import fr.paris.lutece.plugins.tagcloud.business.Tag;
37  import fr.paris.lutece.plugins.tagcloud.business.TagHome;
38  import fr.paris.lutece.plugins.tagcloud.service.RandomTagService;
39  import fr.paris.lutece.portal.business.portlet.Portlet;
40  import fr.paris.lutece.portal.service.plugin.Plugin;
41  import fr.paris.lutece.portal.service.plugin.PluginService;
42  import fr.paris.lutece.util.xml.XmlUtil;
43  
44  import java.util.ArrayList;
45  import java.util.Collection;
46  
47  import javax.servlet.http.HttpServletRequest;
48  
49  
50  /**
51   * This class represents business objects TagCloudPortlet
52   */
53  public class TagCloudPortlet extends Portlet
54  {
55      /////////////////////////////////////////////////////////////////////////////////
56      // Constants
57      private static final String TAG_WEIGHT = "tag-weight";
58      private static final String TAG_NAME = "tag-name";
59      private static final String TAG_URL = "tag-url";
60      private static final String TAG_PORTLET_TAGCLOUD = "cloud";
61      private static final String TAG = "tag";
62  
63      // Variables declarations
64      private int _nIdPortlet;
65      private int _nIdCloud;
66  
67      /**
68       * Sets the identifier of the portlet type to value specified
69       */
70      public TagCloudPortlet(  )
71      {
72          setPortletTypeId( TagCloudPortletHome.getInstance(  ).getPortletTypeId(  ) );
73      }
74  
75      /**
76       * Returns the Xml code of the TagCloud portlet with XML heading
77       *
78       * @param request The HTTP servlet request
79       * @return the Xml code of the TagCloud portlet
80       */
81      public String getXmlDocument( HttpServletRequest request )
82      {
83          return XmlUtil.getXmlHeader(  ) + getXml( request );
84      }
85  
86      /**
87       * Returns the Xml code of the TagCloud portlet without XML heading
88       *
89       * @param request The HTTP servlet request
90       * @return the Xml code of the TagCloud portlet content
91       */
92      public String getXml( HttpServletRequest request )
93      {
94          StringBuffer strXml = new StringBuffer(  );
95          Plugin plugin = PluginService.getPlugin( "tagcloud" );
96  
97          //Fetch the cloud id from the portlet
98          Collection<Integer> listClouds = TagCloudPortletHome.findTagCloudsInPortlet( this.getId(  ) );
99          XmlUtil.beginElement( strXml, TAG_PORTLET_TAGCLOUD );
100 
101         for ( Integer tagCloudId : listClouds )
102         {
103             ArrayList<Tag> listTags = TagHome.findTagsByCloud( tagCloudId.intValue(  ), plugin );
104             RandomTagServiceud/service/RandomTagService.html#RandomTagService">RandomTagService service = new RandomTagService(  );
105 
106             //If no tag is present in a tag 
107             if ( ( listTags != null ) && !listTags.isEmpty(  ) )
108             {
109                 listTags = service.transform( listTags );
110 
111                 for ( Tag tag : listTags )
112                 {
113                     XmlUtil.beginElement( strXml, TAG );
114                     XmlUtil.addElement( strXml, TAG_NAME, tag.getTagName(  ) );
115                     XmlUtil.addElement( strXml, TAG_URL, tag.getTagUrl(  ) );
116                     XmlUtil.addElement( strXml, TAG_WEIGHT, tag.getTagWeight(  ) );
117                     XmlUtil.endElement( strXml, TAG );
118                 }
119             }
120             else
121             {
122                 XmlUtil.beginElement( strXml, TAG );
123                 XmlUtil.addElement( strXml, TAG_NAME, "" );
124                 XmlUtil.addElement( strXml, TAG_URL, "" );
125                 XmlUtil.addElement( strXml, TAG_WEIGHT, "" );
126                 XmlUtil.endElement( strXml, TAG );
127             }
128         }
129 
130         XmlUtil.endElement( strXml, TAG_PORTLET_TAGCLOUD );
131 
132         /**/
133         return addPortletTags( strXml );
134     }
135 
136     /**
137      * Updates the current instance of the TagCloud portlet object
138      */
139     public void update(  )
140     {
141         TagCloudPortletHome.getInstance(  ).update( this );
142     }
143 
144     /**
145      * Removes the current instance of the TagCloud object
146      */
147     public void remove(  )
148     {
149         TagCloudPortletHome.getInstance(  ).remove( this );
150     }
151 
152     /**
153      * Returns the IdPortlet
154      * @return The IdPortlet
155      */
156     public int getIdPortlet(  )
157     {
158         return _nIdPortlet;
159     }
160 
161     /**
162      * Sets the IdPortlet
163      * @param nIdPortlet The IdPortlet
164      */
165     public void setIdPortlet( int nIdPortlet )
166     {
167         _nIdPortlet = nIdPortlet;
168     }
169 
170     /**
171      * Returns the IdCloud
172      * @return The IdCloud
173      */
174     public int getIdCloud(  )
175     {
176         return _nIdCloud;
177     }
178 
179     /**
180      * Sets the IdCloud
181      * @param nIdCloud The IdCloud
182      */
183     public void setIdCloud( int nIdCloud )
184     {
185         _nIdCloud = nIdCloud;
186     }
187 }