package org.hisp.dhis.configuration;

import org.apache.commons.lang3.StringUtils;

/*
 * Copyright (c) 2004-2016, University of Oslo
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the HISP project nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import org.hisp.dhis.dataelement.DataElementGroup;
import org.hisp.dhis.indicator.IndicatorGroup;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.organisationunit.OrganisationUnitLevel;
import org.hisp.dhis.system.deletion.DeletionHandler;
import org.hisp.dhis.user.UserAuthorityGroup;
import org.hisp.dhis.user.UserGroup;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Chau Thu Tran
 */
public class ConfigurationDeletionHandler
    extends DeletionHandler
{
    // -------------------------------------------------------------------------
    // Dependencies
    // -------------------------------------------------------------------------

    @Autowired
    private ConfigurationService configService;
    
    // -------------------------------------------------------------------------
    // DeletionHandler implementation
    // -------------------------------------------------------------------------

    @Override
    protected String getClassName()
    {
        return Configuration.class.getSimpleName();
    }

    @Override
    public String allowDeleteUserGroup( UserGroup userGroup )
    {
        UserGroup feedbackRecipients = configService.getConfiguration().getFeedbackRecipients();
        
        return feedbackRecipients != null ? StringUtils.EMPTY : null;
    }

    @Override
    public String allowDeleteDataElementGroup( DataElementGroup dataElementGroup )
    {
        DataElementGroup infrastructuralDataElements = configService.getConfiguration().getInfrastructuralDataElements();
        
        return infrastructuralDataElements != null ? StringUtils.EMPTY : null;
    }
    
    @Override
    public String allowDeleteIndicatorGroup( IndicatorGroup indicatorGroup )
    {
        IndicatorGroup infrastructuralIndicators = configService.getConfiguration().getInfrastructuralIndicators();
        
        return infrastructuralIndicators != null ? StringUtils.EMPTY : null;
    }
    
    @Override
    public String allowDeleteOrganisationUnitLevel( OrganisationUnitLevel level )
    {
        OrganisationUnitLevel offlineLevel = configService.getConfiguration().getOfflineOrganisationUnitLevel();
        
        return offlineLevel != null ? StringUtils.EMPTY : null;
    }
    
    @Override
    public String allowDeleteOrganisationUnit( OrganisationUnit organisationUnit )
    {
        OrganisationUnit selfRegOrgUnit = configService.getConfiguration().getSelfRegistrationOrgUnit();
        
        return selfRegOrgUnit != null ? StringUtils.EMPTY : null;
    }

    @Override
    public String allowDeleteUserAuthorityGroup( UserAuthorityGroup userAuthorityGroup )
    {
        UserAuthorityGroup selfRegRole = configService.getConfiguration().getSelfRegistrationRole();
        
        return selfRegRole != null ? StringUtils.EMPTY : null;
    }    
}

@@ -2994,16 +2994,18 @@
   return
+ (
  feedbac
@@ -3015,32 +3015,76 @@
 cipients != null
+ && feedbackRecipients.equals( userGroup ) )
  ? StringUtils.E
@@ -3351,32 +3351,34 @@
 %0D%0A        return
+ (
  infrastructural
@@ -3389,32 +3389,92 @@
 Elements != null
+ && infrastructuralDataElements.equals( dataElementGroup ) )
  ? StringUtils.E
@@ -3741,16 +3741,18 @@
   return
+ (
  infrast
@@ -3769,32 +3769,88 @@
 dicators != null
+ && infrastructuralIndicators.equals( indicatorGroup ) )
  ? StringUtils.E
@@ -4119,16 +4119,18 @@
   return
+ (
  offline
@@ -4134,32 +4134,66 @@
 ineLevel != null
+ && offlineLevel.equals( level ) )
  ? StringUtils.E
@@ -4427,32 +4427,24 @@
 OrgUnit();%0D%0A
-        
 %0D%0A        re
@@ -4439,32 +4439,34 @@
 %0D%0A        return
+ (
  selfRegOrgUnit 
@@ -4464,32 +4464,79 @@
 gOrgUnit != null
+ && selfRegOrgUnit.equals( organisationUnit ) )
  ? StringUtils.E
@@ -4796,16 +4796,18 @@
   return
+ (
  selfReg
@@ -4818,16 +4818,62 @@
  != null
+ && selfRegRole.equals( userAuthorityGroup ) )
  ? Strin

Fix ConfigurationDeletionHandler: actually evaluate arguments. Previously failed as long as the config was set, regardless of its value (#492)