ProgressManagerService.java

  1. /*
  2.  * Copyright (c) 2002-2022, 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. /*
  35.  * To change this license header, choose License Headers in Project Properties.
  36.  * To change this template file, choose Tools | Templates
  37.  * and open the template in the editor.
  38.  */
  39. package fr.paris.lutece.portal.service.progressmanager;

  40. import fr.paris.lutece.portal.business.progressmanager.ProgressFeed;
  41. import java.util.ArrayList;
  42. import java.util.Arrays;
  43. import java.util.HashMap;
  44. import java.util.List;
  45. import java.util.Map;
  46. import java.util.UUID;

  47. /**
  48.  *
  49.  * @author sleridon
  50.  */
  51. public final class ProgressManagerService
  52. {

  53.     private static ProgressManagerService _singleton;
  54.     private static Map<String, ProgressFeed> _progressFeeds;

  55.     /**
  56.      * Private constructor
  57.      */
  58.     private ProgressManagerService( )
  59.     {
  60.     }

  61.     /**
  62.      * Returns the unique instance of the service
  63.      *
  64.      * @return The instance of the service
  65.      */
  66.     public static synchronized ProgressManagerService getInstance( )
  67.     {
  68.         if ( _singleton == null )
  69.         {
  70.             _singleton = new ProgressManagerService( );
  71.             _progressFeeds = new HashMap<>( );
  72.         }
  73.         return _singleton;
  74.     }

  75.     /**
  76.      * ge progress feeds
  77.      *
  78.      * @return the progress feeds
  79.      */
  80.     public Map<String, ProgressFeed> getProgressFeeds( )
  81.     {
  82.         Map<String, ProgressFeed> feedMap = new HashMap<>( );
  83.         feedMap.putAll( _progressFeeds );

  84.         return feedMap;
  85.     }

  86.     /**
  87.      * register new progress feed returns a generated token to identify the feed for rest webservice
  88.      *
  89.      * @param strProgressFeedName
  90.      * @param nTotalItems
  91.      * @return the generated token
  92.      */
  93.     public String registerFeed( String strProgressFeedName, int nTotalItems )
  94.     {
  95.         ProgressFeed feed = new ProgressFeed( );
  96.         feed.setId( strProgressFeedName );
  97.         feed.setNbItemTotal( nTotalItems );

  98.         String strToken = UUID.randomUUID( ).toString( );
  99.         feed.setToken( strToken );

  100.         _progressFeeds.put( strToken, feed );

  101.         return feed.getToken( );
  102.     }

  103.     /**
  104.      * unregister progress feed
  105.      *
  106.      * @param strFeedToken
  107.      */
  108.     public void unRegisterFeed( String strFeedToken )
  109.     {
  110.         _progressFeeds.remove( strFeedToken );
  111.     }

  112.     /**
  113.      * register new progress feed
  114.      *
  115.      * @param strFeedToken
  116.      * @return
  117.      */
  118.     public boolean isRegistred( String strFeedToken )
  119.     {
  120.         return ( _progressFeeds.get( strFeedToken ) != null );
  121.     }

  122.     /**
  123.      * increment nb of success items
  124.      *
  125.      * @param strFeedToken
  126.      * @param nSuccessItems
  127.      */
  128.     public synchronized void incrementSuccess( String strFeedToken, int nSuccessItems )
  129.     {
  130.         if ( _progressFeeds.get( strFeedToken ) != null )
  131.         {
  132.             _progressFeeds.get( strFeedToken ).addSuccessItems( nSuccessItems );
  133.         }
  134.     }

  135.     /**
  136.      * increment nb of success items
  137.      *
  138.      * @param strFeedToken
  139.      * @param nFailureItems
  140.      */
  141.     public synchronized void incrementFailure( String strFeedToken, int nFailureItems )
  142.     {
  143.         if ( _progressFeeds.get( strFeedToken ) != null )
  144.         {
  145.             _progressFeeds.get( strFeedToken ).addFailureItems( nFailureItems );
  146.         }
  147.     }

  148.     /**
  149.      * add report line
  150.      *
  151.      * @param strFeedToken
  152.      * @param strReportLine
  153.      */
  154.     public void addReport( String strFeedToken, String strReportLine )
  155.     {
  156.         if ( _progressFeeds.get( strFeedToken ) != null )
  157.         {
  158.             _progressFeeds.get( strFeedToken ).addReport( strReportLine );
  159.         }
  160.     }

  161.     /**
  162.      * get the progress status in pourcent
  163.      *
  164.      * @param strFeedToken
  165.      * @return
  166.      */
  167.     public int getProgressStatus( String strFeedToken )
  168.     {
  169.         if ( _progressFeeds.get( strFeedToken ) != null )
  170.         {
  171.             ProgressFeed feed = _progressFeeds.get( strFeedToken );
  172.             if ( feed.getNbItemTotal( ) > 0 )
  173.             {
  174.                 if ( feed.getNbItemTotal( ) <= feed.getNbItemSuccess( ) + feed.getNbItemFailure( ) )
  175.                 {
  176.                     return 100;
  177.                 }
  178.                 else
  179.                 {
  180.                     return (int) ( ( feed.getNbItemSuccess( ) + feed.getNbItemFailure( ) ) * 100.0 / feed.getNbItemTotal( ) + 0.5 );
  181.                 }
  182.             }
  183.         }

  184.         return -1;
  185.     }

  186.     /**
  187.      * get the success nb
  188.      *
  189.      * @param strFeedToken
  190.      * @return the success nb
  191.      */
  192.     public int getSuccessNb( String strFeedToken )
  193.     {
  194.         if ( _progressFeeds.get( strFeedToken ) != null )
  195.         {
  196.             return _progressFeeds.get( strFeedToken ).getNbItemSuccess( );
  197.         }

  198.         return -1;
  199.     }

  200.     /**
  201.      * get the failure nb
  202.      *
  203.      * @param strFeedToken
  204.      * @return the failure nb
  205.      */
  206.     public int getFailureNb( String strFeedToken )
  207.     {
  208.         if ( _progressFeeds.get( strFeedToken ) != null )
  209.         {
  210.             return _progressFeeds.get( strFeedToken ).getNbItemFailure( );
  211.         }

  212.         return -1;
  213.     }

  214.     /**
  215.      * get the report list
  216.      *
  217.      * @param strFeedToken
  218.      * @return the failure nb
  219.      */
  220.     public List<String> getReport( String strFeedToken )
  221.     {
  222.         return getReport( strFeedToken, -1 );
  223.     }

  224.     /**
  225.      * get the report list
  226.      *
  227.      * @param strFeedToken
  228.      * @param iFromLine
  229.      * @return the failure nb
  230.      */
  231.     public List<String> getReport( String strFeedToken, int iFromLine )
  232.     {
  233.         if ( _progressFeeds.get( strFeedToken ) != null )
  234.         {
  235.             List<String> reportList = _progressFeeds.get( strFeedToken ).getReportList( );

  236.             if ( iFromLine > 0 && iFromLine < reportList.size( ) )
  237.             {
  238.                 // clone to avoid ConcurrentModificationException
  239.                 ArrayList<String> clonedReport = (ArrayList<String>) ( (ArrayList<String>) reportList ).clone( );
  240.                 return clonedReport.subList( iFromLine - 1, clonedReport.size( ) - 1 );
  241.             }
  242.             else
  243.                 if ( iFromLine >= reportList.size( ) )
  244.                 {
  245.                     return new ArrayList<String>( );
  246.                 }
  247.                 else
  248.                 {
  249.                     return reportList;
  250.                 }
  251.         }

  252.         return null;
  253.     }

  254.     /**
  255.      * update feed total item number
  256.      *
  257.      * @param strFeedToken
  258.      * @param nTotalItems
  259.      * @return true if the feed exists
  260.      */
  261.     public boolean initFeed( String strFeedToken, int nTotalItems )
  262.     {
  263.         if ( _progressFeeds.get( strFeedToken ) != null )
  264.         {
  265.             _progressFeeds.get( strFeedToken ).setNbItemTotal( nTotalItems );
  266.             return true;
  267.         }
  268.         else
  269.         {
  270.             return false;
  271.         }
  272.     }
  273. }