View Javadoc
1   /*
2    * To change this license header, choose License Headers in Project Properties.
3    * To change this template file, choose Tools | Templates
4    * and open the template in the editor.
5    */
6   package fr.paris.lutece.plugins.adminwall.service;
7   
8   import fr.paris.lutece.plugins.adminwall.business.Hashtag;
9   import fr.paris.lutece.plugins.adminwall.business.HashtagHome;
10  import fr.paris.lutece.plugins.adminwall.business.Link;
11  import fr.paris.lutece.plugins.adminwall.business.LinkHome;
12  import fr.paris.lutece.plugins.adminwall.business.Post;
13  
14  import java.util.regex.Matcher;
15  import java.util.regex.Pattern;
16  
17  
18  /**
19   *
20   * @author Brasya
21   */
22  public final class AdminWallService
23  {
24      /**
25       * Creates a new instance of AdminWallService
26       */
27      private AdminWallService(  )
28      {
29      }
30  
31      /**
32       * Activate the URL in the post
33       *
34       * @param post The Post
35       */
36      public static void activateURL( Post post )
37      {
38          String inputUrl = post.getContenu(  );
39          String strPatternUrl = "(((https?)://)?([\\w\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w\\.-]*)*\\/?)";
40          Pattern pattUrl = Pattern.compile( strPatternUrl );
41          Matcher matchUrl = pattUrl.matcher( inputUrl );
42          StringBuffer strBuffUrl = new StringBuffer(  );
43          boolean flagUrl = false;
44  
45          while ( ( flagUrl = matchUrl.find(  ) ) )
46          {
47              String lien = matchUrl.group( 1 );
48              String http = matchUrl.group( 2 );
49              String strHref = lien;
50  
51              if ( http == null )
52              { //Detection du protocole
53                  strHref = "http://" + lien;
54              }
55  
56              matchUrl.appendReplacement( strBuffUrl,
57                  "<a href=\"" + strHref + "\" alt=lien url target=\"_blank\">" + lien + "</a>" );
58          }
59  
60          matchUrl.appendTail( strBuffUrl );
61  
62          String strUrl = strBuffUrl.toString(  );
63          post.setContenu( strUrl );
64      }
65  
66      /**
67       * Activate tag filter links in the post
68       *
69       * @param post The Post
70       */
71      public static void activateHashtag( Post post )
72      {
73          String inputTag = post.getContenu(  );
74          String strPatternTag = "(#[\\w\\u00C0-\\u00FF\\u0153]+)";
75          Pattern pattTag = Pattern.compile( strPatternTag );
76          Matcher matchTag = pattTag.matcher( inputTag );
77          StringBuffer strBuffTag = new StringBuffer(  );
78          boolean flagTag = false;
79  
80          while ( ( flagTag = matchTag.find(  ) ) )
81          {
82              String tag = matchTag.group(  ).replace( "#", "" );
83              String mot = matchTag.group(  );
84              matchTag.appendReplacement( strBuffTag,
85                  "<a href=\"jsp/admin/plugins/adminwall/ManageWall.jsp?view=managePosts&tag=" + tag + "\">" + mot +
86                  "</a>" );
87          }
88  
89          matchTag.appendTail( strBuffTag );
90  
91          String strTag = strBuffTag.toString(  );
92          post.setContenu( strTag );
93      }
94  
95      //Detecte les Hashtags dans le post et insert dans la DB
96      /**
97       * Detect hashtags from the post and insert them in the database
98       *
99       * @param post The Post
100      */
101     public static void detectHashtag( Post post )
102     {
103         Pattern pattTag = Pattern.compile( "#[\\w\\u00C0-\\u00FF\\u0153]+" );
104         Matcher matchTag = pattTag.matcher( post.getContenu(  ) );
105 
106         while ( matchTag.find(  ) )
107         {
108             String[] tab = new String[2];
109             tab = matchTag.group(  ).split( "#", 2 );
110 
111             Hashtag hashtag = new Hashtag(  );
112             hashtag.setTag( tab[1] );
113             HashtagHome.create( hashtag );
114 
115             /*Insertion du Link*/
116             Link link = new Link(  );
117             link.setPost( post.getIdPost(  ) );
118             link.setHashtag( hashtag.getIdHashtag(  ) );
119             LinkHome.create( link );
120 
121             link = null;
122             hashtag = null;
123         }
124     }
125 }