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