/**
 * Adds an application folder to the publisher folder if it's not yet added
 * @param mixed $applicationId
 * @param mixed $publisherFolderId
 * @param eZCLI $cli
 */
function alignApplicationWithPublisherFolder( $applicationId, $publisherFolderId, eZCLI $cli )
{
    $application = $applicationId;
    $publisherFolder = $publisherFolderId;
    if (! $application instanceof ApplicationLocalized )
    {
        $application = ApplicationLocalized::getLocalizedApplicationById( $applicationId, false );
    }
    if ( $application == null )
    {
        throw new Exception( "Application localized with id {$applicationId} does not exist." );
    }

    if (! $publisherFolder instanceof MMSynchPublisherFolder )
    {
        $publisherFolder = MMSynchPublisherFolder::fetch( $publisherFolderId );
    }
    if ( $publisherFolder == null )
    {
        throw new Exception( "Publisher folder with id {$publisherFolderId} does not exist." );
    }

    $remoteId = 'application_folder-' . $application->cluster_identifier . '-' . $application->applicationObject->identifier;
    $applicationFolderObject = eZContentObject::fetchByRemoteID( $remoteId );
    if ( $applicationFolderObject == null )
    {
        throw new Exception( "Application folder object with remote id {$remoteId} doesn't exist" );
    }

    $applicationFolderId = $applicationFolderObject->attribute( 'id' );
    //use getContent with true to create problems
    $path = $publisherFolder->getContent( )->fields->path->toString();
    if ( $path )
    {
        $contentobject = $publisherFolder->getContent()->getRawContentObject();

        $languages = $contentobject->availableLanguages();

        foreach ( $languages as $language )
        {
            $dataMap = $contentobject->dataMap();
            $contentObjectAttributeTargetCS = $dataMap['target_cs'];
            $objectIds = explode( '-', $contentObjectAttributeTargetCS->toString() );

            if ( !in_array( $applicationFolderId, $objectIds ) )
            {
                $objectIds[] = $applicationFolderId;
                $contentObjectAttributeTargetCS->fromString( implode( '-', $objectIds ) );
                $contentObjectAttributeTargetCS->store();
            } 
            else
            {
                $cli->notice( "Ommiting application folder with path {$remoteId}" );
            }
        }
    }
}
    /**
     * @param int $applicationLocalizedId
     * @param bool $force
     * @param bool $store
     * @return ApplicationLocalized
     */
    public static function buildLocalizedApplicationByLocalizedId($applicationLocalizedId, $force = false, $store = true)
    {
        $localizedApplication = null;
        if( !$force )
            $localizedApplication = CacheApplicationTool::getValue('applicationNameByLocalizedId', $applicationLocalizedId);

        if ( !($localizedApplication instanceof ApplicationLocalized) || $force )
        {
            $localizedApplication = ApplicationLocalized::getLocalizedApplicationById($applicationLocalizedId);

            if ( $localizedApplication instanceof ApplicationLocalized )
            {
                self::buildCache($localizedApplication, $store);

                return $localizedApplication;
            }
            else
            {
                return null;
            }
        }
        else
        {
            return $localizedApplication;
        }
    }