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.cms.lock;
017
018import java.io.IOException;
019import java.time.Duration;
020import java.time.LocalDateTime;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.ProcessingException;
026import org.apache.cocoon.xml.XMLUtils;
027import org.xml.sax.SAXException;
028import org.xml.sax.helpers.AttributesImpl;
029
030import org.ametys.cms.repository.Content;
031import org.ametys.core.right.RightManager;
032import org.ametys.core.right.RightManager.RightResult;
033import org.ametys.core.user.User;
034import org.ametys.core.user.UserIdentity;
035import org.ametys.core.user.UserManager;
036import org.ametys.core.util.cocoon.AbstractCurrentUserProviderServiceableGenerator;
037import org.ametys.plugins.repository.lock.LockHelper;
038import org.ametys.plugins.repository.lock.LockableAmetysObject;
039import org.ametys.plugins.repository.lock.UnlockHelper;
040
041/**
042 * List contents that can be unlocked.
043 */
044public class UnlockGenerator extends AbstractCurrentUserProviderServiceableGenerator
045{
046    /** The unlock helper */
047    protected UnlockHelper _unlockHelper;
048    /** The rights manager */
049    protected RightManager _rightManager;
050    /** The users manager */
051    protected UserManager _userManager;
052
053    @Override
054    public void service(ServiceManager sManager) throws ServiceException
055    {
056        super.service(sManager);
057
058        _unlockHelper = (UnlockHelper) manager.lookup(UnlockHelper.ROLE);
059        _rightManager = (RightManager) manager.lookup(RightManager.ROLE);
060        _userManager = (UserManager) manager.lookup(UserManager.ROLE);
061    }
062
063    public void generate() throws IOException, SAXException, ProcessingException
064    {
065        contentHandler.startDocument();
066        XMLUtils.startElement(contentHandler, "List");
067        
068        Map<LockableAmetysObject, LocalDateTime> lockedObjects = _getLockedObjects();
069        
070        _saxLockedContents(lockedObjects);
071        
072        XMLUtils.endElement(contentHandler, "List");
073        contentHandler.endDocument();
074    }
075    
076    /**
077     * Get the locked objects.
078     * @return the locked objects.
079     */
080    protected Map<LockableAmetysObject, LocalDateTime> _getLockedObjects()
081    {
082        return _unlockHelper.getLockedObjects();
083    }
084    
085    /**
086     * SAX the locked objects.
087     * @param lockedObjects the locked objects.
088     * @throws SAXException if an error occurs generating the XML data.
089     */
090    protected void _saxLockedContents(Map<LockableAmetysObject, LocalDateTime> lockedObjects) throws SAXException
091    {
092        boolean unlockingActivated = _unlockHelper.isUnlockingActivated();
093    
094        // Get the current user
095        UserIdentity userIdentity = _getCurrentUser();
096    
097        boolean canSeeLocked = _rightManager.hasRight(userIdentity, "CMS_Rights_SeeLocked", "/cms") == RightResult.RIGHT_ALLOW;
098        boolean canUnlockAll = _rightManager.hasRight(userIdentity, "CMS_Rights_UnlockAll", "/cms") == RightResult.RIGHT_ALLOW;
099        long period = _unlockHelper.getTimeBeforeUnlock();
100    
101        for (LockableAmetysObject ametysObject : lockedObjects.keySet())
102        {
103            // Filter contents
104            if (ametysObject instanceof Content)
105            {
106                UserIdentity lockOwner = ametysObject.getLockOwner();
107                boolean isLockOwner = LockHelper.isLockOwner(ametysObject, userIdentity);
108                
109                if (isLockOwner || canSeeLocked)
110                {
111                    LocalDateTime lockTime = lockedObjects.get(ametysObject);
112                    
113                    AttributesImpl attrs = new AttributesImpl();
114                    attrs.addAttribute("", "id", "id", "CDATA", ametysObject.getId());
115                    attrs.addAttribute("", "title", "title", "CDATA", ((Content) ametysObject).getTitle());
116                    
117                    if (unlockingActivated)
118                    {
119                        long unlockDuration = Duration.between(LocalDateTime.now(), lockTime.plusHours(period)).toMillis();
120                        attrs.addAttribute("", "unlockTime", "unlockTime", "CDATA", Long.toString(unlockDuration));
121                    }
122                    Duration sinceDuration = Duration.between(lockTime, LocalDateTime.now());
123                    attrs.addAttribute("", "since", "since", "CDATA", Long.toString(sinceDuration.toMillis()));
124                    
125                    User user = null;
126                    if (lockOwner != null)
127                    {
128                        user = _userManager.getUser(lockOwner.getPopulationId(), lockOwner.getLogin());
129                    }
130                    String lockBy = user != null ? user.getFullName() + " (" + user.getIdentity().getLogin() + ")" : (lockOwner != null ? lockOwner.getLogin() : "Anonymous");
131                    attrs.addAttribute("", "lockBy", "lockBy", "CDATA", lockBy);
132                    
133                    if (canUnlockAll || isLockOwner)
134                    {
135                        attrs.addAttribute("", "canUnlock", "canUnlock", "CDATA", "true");
136                    }
137                    
138                    if (isLockOwner)
139                    {
140                        attrs.addAttribute("", "isLockOwner", "isLockOwner", "CDATA", "true");
141                    }
142                    
143                    XMLUtils.createElement(contentHandler, "LockedContent", attrs);
144                }
145            }
146        }
147    }
148
149}