001/*
002 *  Copyright 2016 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.plugins.forms.content;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import javax.jcr.ItemNotFoundException;
024import javax.jcr.Node;
025import javax.jcr.PathNotFoundException;
026
027import org.apache.avalon.framework.parameters.Parameters;
028import org.apache.avalon.framework.service.ServiceException;
029import org.apache.avalon.framework.service.ServiceManager;
030import org.apache.cocoon.ProcessingException;
031import org.apache.cocoon.acting.ServiceableAction;
032import org.apache.cocoon.environment.ObjectModelHelper;
033import org.apache.cocoon.environment.Redirector;
034import org.apache.cocoon.environment.Request;
035import org.apache.cocoon.environment.SourceResolver;
036
037import org.ametys.core.cocoon.JSonReader;
038import org.ametys.core.user.User;
039import org.ametys.core.user.UserManager;
040import org.ametys.plugins.forms.content.jcr.FormPropertiesManager;
041import org.ametys.plugins.forms.content.table.FormTableManager;
042
043/**
044 * Generator listing all of the forms that are no longer referenced by any content
045 */
046public class GetOrphanFormsAction extends ServiceableAction
047{
048    /** Component gathering methods to query forms in JCR */
049    private FormPropertiesManager _formPropertiesManager;
050    
051    /** Component gathering methods to manipulate SQL forms */
052    private FormTableManager _formTableManager;
053    
054    /** The users manager. */
055    private UserManager _userManager;
056
057    @Override
058    public void service(ServiceManager serviceManager) throws ServiceException
059    {
060        _formPropertiesManager = (FormPropertiesManager) serviceManager.lookup(FormPropertiesManager.ROLE);
061        _formTableManager = (FormTableManager) serviceManager.lookup(FormTableManager.ROLE);
062        _userManager = (UserManager) serviceManager.lookup(UserManager.ROLE);
063    }
064    
065    @Override
066    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
067    {
068        Map<String, Object> result = new HashMap<> ();
069        try
070        {
071            String formId = null;
072            Form form = null;
073            
074            List<String> tableNames = _formTableManager.getTableNames();
075            
076            List<Map<String, Object>> tableList = new ArrayList<> ();
077            for (String tableName : tableNames)
078            {
079                Map<String, Object> table = new HashMap<> ();
080                
081                formId = tableName.substring(FormTableManager.TABLE_PREFIX.length()); 
082                if (formId.length() == 0)
083                {
084                    getLogger().error("Find a SQL form table with a empty id '" + tableName + "'. It will be ignored. This table should be deleted manually.");
085                }
086                else
087                {
088                    form = _formPropertiesManager.getForm(formId);
089                    if (form == null)
090                    {
091                        Node frozenContentNode = _formPropertiesManager.getMostRecentFormFrozenContent(formId);
092                        if (frozenContentNode != null)
093                        {
094                            String contentUUId = frozenContentNode.getProperty("jcr:frozenUuid").getString();
095                            String title = frozenContentNode.getProperty("ametys:title").getString();
096                            
097                            table.put("contentTitle", title);
098                            
099                            if (frozenContentNode.hasProperty("ametys:site"))
100                            {
101                                table.put("siteName", frozenContentNode.getProperty("ametys:site").getString());
102                            }
103                            
104                            try
105                            {
106                                Node contributorNode = frozenContentNode.getNode("ametys:contributor");
107                                
108                                String contributorLogin = contributorNode.getProperty("ametys:login").getString();
109                                User user = _userManager.getUser(contributorNode.getProperty("ametys:population").getString(), contributorNode.getProperty("ametys:login").getString());
110                                table.put("lastContributor", user != null ? user.getFullName() : contributorLogin);
111                            }
112                            catch (PathNotFoundException e)
113                            {
114                                // For legacy purposes: orphan forms since a 3.x version
115                                String contributorLogin = frozenContentNode.getProperty("ametys:contributor").getString();
116                                table.put("lastContributor", contributorLogin);
117                            }
118                            
119                            
120                            boolean stillExists = true;
121                            try
122                            {
123                                frozenContentNode.getSession().getNodeByIdentifier(contentUUId);
124                            }
125                            catch (ItemNotFoundException e)
126                            {
127                                stillExists = false;
128                            }
129                            
130                            table.put("stillExists", stillExists);
131                        }
132                        
133                        // Table attrs
134                        table.put("name", tableName);
135                        table.put("total", Integer.toString(_formTableManager.getTotalSubmissions(formId)));
136                        tableList.add(table);
137                    }
138                }
139            }
140            result.put("tables", tableList);
141        }
142        catch (Exception e)
143        {
144            throw new ProcessingException("An error occurred while retrieving the orphan forms.", e);
145        }
146        
147        
148        Request request = ObjectModelHelper.getRequest(objectModel);
149        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
150        return EMPTY_MAP;
151    }
152}