001/*
002 *  Copyright 2010 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.skinfactory.actions;
017
018import java.io.IOException;
019import java.nio.file.Path;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.acting.ServiceableAction;
026import org.apache.cocoon.environment.Request;
027
028import org.ametys.core.cocoon.JSonReader;
029import org.ametys.core.user.CurrentUserProvider;
030import org.ametys.core.user.User;
031import org.ametys.core.user.UserIdentity;
032import org.ametys.core.user.UserManager;
033import org.ametys.plugins.skincommons.SkinEditionHelper;
034import org.ametys.plugins.skincommons.SkinLockManager;
035import org.ametys.skinfactory.SkinFactoryComponent;
036import org.ametys.web.repository.site.SiteManager;
037import org.ametys.web.skin.SkinModelsManager;
038import org.ametys.web.skin.SkinsManager;
039
040/**
041 * Abstract class for manipulating skin directory
042 *
043 */
044public abstract class AbstractSkinAction extends ServiceableAction
045{
046    /** Constant for skin editor tool id */
047    public static final String SKIN_FACTORY_TOOL_ID = "uitool-skinfactory";
048    
049    /** The cocoon context */
050    protected org.apache.cocoon.environment.Context _cocoonContext;
051    /** The user provider */
052    protected CurrentUserProvider _userProvider;
053    /** The sites manager */
054    protected SiteManager _siteManager;
055    /** The skin factory component */
056    protected SkinFactoryComponent _skinFactoryManager;
057    /** The models manager */
058    protected SkinModelsManager _modelsManager;
059    /** The lock manager */
060    protected SkinLockManager _lockManager;
061    /** The skin edition helper */
062    protected SkinEditionHelper _skinHelper;
063    /** The skin manager */
064    protected SkinsManager _skinsManager;
065    /** The users manager */
066    protected UserManager _userManager;
067    
068    @Override
069    public void service(ServiceManager smanager) throws ServiceException
070    {
071        super.service(smanager);
072        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
073        _skinFactoryManager = (SkinFactoryComponent) smanager.lookup(SkinFactoryComponent.ROLE);
074        _modelsManager = (SkinModelsManager) smanager.lookup(SkinModelsManager.ROLE);
075        _lockManager = (SkinLockManager) smanager.lookup(SkinLockManager.ROLE);
076        _skinHelper = (SkinEditionHelper) smanager.lookup(SkinEditionHelper.ROLE);
077        _skinsManager = (SkinsManager) smanager.lookup(SkinsManager.ROLE);
078        _userManager = (UserManager) smanager.lookup(UserManager.ROLE);
079    }
080    
081    /**
082     * Determines the skin directory is locked. If no, the lock owner is set in JSON map request attribute
083     * @param request The request 
084     * @param skinDir The skin directory
085     * @return <code>true</code> if the current user can write.
086     * @throws IOException if an exception occurs while manipulating the skin directory 
087     */
088    protected boolean checkLock (Request request, Path skinDir) throws IOException
089    {
090        if (!_lockManager.canWrite(skinDir))
091        {
092            Map<String, Object> result = new HashMap<>();
093            
094            UserIdentity lockOwner = _lockManager.getLockOwner(skinDir);
095            User user = _userManager.getUser(lockOwner.getPopulationId(), lockOwner.getLogin());
096
097            result.put("isLocked", true);
098            result.put("lockOwner", user != null ? user.getFullName() + " (" + lockOwner + ")" : lockOwner);
099            
100            request.setAttribute(JSonReader.OBJECT_TO_READ, result);
101            return false;
102        }
103        return true;
104    }
105    
106    /**
107     * Determines if the model still exists
108     * @param request The request
109     * @param modelName The model name
110     * @return <code>true</code> if the model still exists.
111     */
112    protected boolean checkModelExists (Request request, String modelName)
113    {
114        Map<String, Object> result = new HashMap<>();
115        
116        boolean modelExists = _modelsManager.getModel(modelName) != null;
117        if (!modelExists)
118        {
119            result.put("unknownModel", true);
120            result.put("modelName", modelName);
121            
122            request.setAttribute(JSonReader.OBJECT_TO_READ, result);
123            return false;
124        }
125        
126        return true;
127    }
128}