View Javadoc
1   /*
2    * Copyright (c) 2002-2023, City of 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.broadcastproxy.service;
35  
36  import java.util.Arrays;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  import org.apache.commons.lang3.StringUtils;
42  
43  import com.fasterxml.jackson.databind.JsonNode;
44  import com.fasterxml.jackson.databind.ObjectMapper;
45  
46  import fr.paris.lutece.plugins.broadcastproxy.business.SubscriptionLink;
47  import fr.paris.lutece.plugins.broadcastproxy.business.SubscriptionLinkHome;
48  import fr.paris.lutece.plugins.broadcastproxy.business.providers.dolist.DolistConstants;
49  import fr.paris.lutece.portal.service.daemon.Daemon;
50  import fr.paris.lutece.portal.service.util.AppLogService;
51  import fr.paris.lutece.portal.service.util.AppPropertiesService;
52  
53  /**
54   * 
55   * Load the list of subscriptions
56   *
57   */
58  public class BroadcastDaemon extends Daemon
59  {
60  
61      // Constants
62      private static final String JSON_NODE_ITEMLIST              = AppPropertiesService.getProperty( "dolist.jsonNode.ItemList" );
63      private static final String JSON_NODE_GROUP                 = AppPropertiesService.getProperty( "dolist.jsonNode.item.Group" );
64      private static final String JSON_NODE_GROUP_ID              = AppPropertiesService.getProperty( "dolist.jsonNode.item.GroupId" );
65      private static final String JSON_NODE_INTEREST_LIST         = AppPropertiesService.getProperty( "dolist.jsonNode.item.InterestList" );
66      private static final String JSON_NODE_GROUP_NAME            = AppPropertiesService.getProperty( "dolist.jsonNode.group.Name" );
67      private static final String JSON_NODE_DELETE_DATE           = AppPropertiesService.getProperty( "dolist.jsonNode.interest.isActive" );
68      private static final String PROPERTY_ACCOUNT_ID             = AppPropertiesService.getProperty( "dolist.CONSTANTE_ACCOUNT_ID" );
69      private static final String PROPERTY_GROUP_IDS              = AppPropertiesService.getProperty( "dolist.list.group_ids" );
70      
71      @Override
72      public void run( )
73      {
74          loadSubscription( PROPERTY_ACCOUNT_ID );
75      }
76  
77      /**
78       * Retrieve subscriptions
79       */
80      private void loadSubscription( String strAccountId )
81      {
82          BroadcastService broadcastService = BroadcastService.getInstance( );
83          String jsonAllSubscriptionsInterest = broadcastService.getAllSubscriptionByGroup( DolistConstants.TYPE_INTEREST, strAccountId );
84          String subscriptionsInJson = broadcastService.getAllSubscriptionByGroup( DolistConstants.TYPE_SUBSCRIPTION, strAccountId );
85  
86          ObjectMapper mapper = new ObjectMapper( );
87          String groupName = StringUtils.EMPTY;
88          Map<String, String> subscriptionsMapIdName = new HashMap<>( );
89          List<String> listGroupIds = Arrays.asList( PROPERTY_GROUP_IDS.split( ";" ) );
90  
91          try
92          {
93              // Get subscriptions data (id and name)
94              JsonNode nodesSub = mapper.readTree( subscriptionsInJson );
95  
96              JsonNode itemListNodeSub = nodesSub.get( JSON_NODE_ITEMLIST );
97  
98              for ( JsonNode node : itemListNodeSub )
99              {
100                 if ( node.get( "IsEnabled" ).asBoolean( ) )
101                 {
102                     subscriptionsMapIdName.put( node.get( JSON_NODE_GROUP_NAME ).asText( ), node.get( "ID" ).asText( ) );
103                 }
104             }
105             
106             //Interests
107             JsonNode nodes = mapper.readTree( jsonAllSubscriptionsInterest );
108             if ( !nodes.get( JSON_NODE_ITEMLIST ).isNull( ) )
109             {
110                 JsonNode itemListNode = nodes.get( JSON_NODE_ITEMLIST );
111 
112                 for ( JsonNode itemNode : itemListNode )
113                 {
114                     JsonNode groupData = itemNode.get( JSON_NODE_GROUP );                    
115                     
116                     if( listGroupIds.contains( groupData.get( JSON_NODE_GROUP_ID ).asText( ) ) )
117                     {
118                         groupName = groupData.get( JSON_NODE_GROUP_NAME ).asText( );
119 
120                         if ( groupName.substring( 0, 1 ).equals( "[" ) && groupName.substring( groupName.length( ) - 1, groupName.length( ) ).equals( "]" ) && groupName.length( ) > 2 )
121                         {
122                             String[] splitDlGrName = groupName.split( "\\]" );
123                             groupName = splitDlGrName[0].substring( 1, splitDlGrName[0].length( ) );
124                         }
125                         
126                         for ( JsonNode node : itemNode.get( JSON_NODE_INTEREST_LIST ) )
127                         {
128                             createSubscriptionLink( subscriptionsMapIdName, groupName, node  );
129                         }
130                     }
131 
132                 }
133             }
134         } catch ( Exception e )
135         {
136             String strError = "Error occured while building list of all subscription.";
137             AppLogService.error( strError + e.getMessage( ), e );
138         }
139     }
140 
141     private void createSubscriptionLink( Map<String, String> subscriptionsMapIdName, String groupName, JsonNode node )
142     {
143         String strName = node.get( JSON_NODE_GROUP_NAME ).asText( );
144 
145         if ( subscriptionsMapIdName.containsKey( strName ) && !node.has( JSON_NODE_DELETE_DATE )
146                 && !SubscriptionLinkHome.findBySubscriptionId( Integer.parseInt( subscriptionsMapIdName.get( strName ) ) ).isPresent( ) )
147         {
148             SubscriptionLinkproxy/business/SubscriptionLink.html#SubscriptionLink">SubscriptionLink subLink = new SubscriptionLink( );
149             
150             subLink.setSubscriptionId( Integer.parseInt( subscriptionsMapIdName.get( strName ) ) );
151             subLink.setInterestId( node.get( "ID" ).asInt( ) );
152             subLink.setGroupId( node.get( "GroupID" ).asInt( ) );
153             subLink.setDescription( StringUtils.EMPTY );
154             subLink.setLabel( strName );
155             subLink.setPictogramme( StringUtils.EMPTY );
156             subLink.setFrequency( StringUtils.EMPTY );
157             subLink.setEnabled( true );
158             subLink.setGroup( groupName );
159 
160             SubscriptionLinkHome.create( subLink );
161         }
162 
163     }
164 
165 }