001/* 002 * Copyright 2015 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.data; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.commons.lang.StringUtils; 026 027import org.ametys.cms.repository.Content; 028import org.ametys.core.ui.Callable; 029import org.ametys.core.ui.SimpleMenu; 030import org.ametys.plugins.forms.Form; 031import org.ametys.plugins.forms.FormsException; 032import org.ametys.plugins.forms.jcr.FormPropertiesManager; 033import org.ametys.plugins.repository.AmetysObjectResolver; 034 035/** 036 * This element creates a content button allowing the export of the form data. 037 */ 038public class FormsContentClientSideElement extends SimpleMenu 039{ 040 /** The form properties manager. */ 041 private FormPropertiesManager _formPropertiesManager; 042 043 /** The Ametys object resolver. */ 044 private AmetysObjectResolver _resolver; 045 046 @Override 047 public void service(ServiceManager smanager) throws ServiceException 048 { 049 super.service(smanager); 050 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 051 _formPropertiesManager = (FormPropertiesManager) smanager.lookup(FormPropertiesManager.ROLE); 052 } 053 054 /** 055 * Get the forms status of contents 056 * @param contentIds the ids of the contents 057 * @return the map of form properties of the contents 058 */ 059 @SuppressWarnings("unchecked") 060 @Callable 061 public Map<String, Object> getStatus(List<String> contentIds) 062 { 063 Map<String, Object> results = new HashMap<>(); 064 065 results.put("forms", new ArrayList<Map<String, Object>>()); 066 067 for (String contentId : contentIds) 068 { 069 Content content = _resolver.resolveById(contentId); 070 try 071 { 072 List<Form> contentForms = _formPropertiesManager.getForms(content); 073 for (Form form : contentForms) 074 { 075 // If the ID or label is blank, the form won't be in the database. 076 if (StringUtils.isNotEmpty(form.getId()) && StringUtils.isNotEmpty(form.getLabel())) 077 { 078 Map<String, String> formParams = new HashMap<> (); 079 formParams.put("id", form.getId()); 080 formParams.put("label", form.getLabel()); 081 formParams.put("content-id", contentId); 082 formParams.put("content-title", content.getTitle()); 083 084 List<Map<String, String>> forms = (List<Map<String, String>>) results.get("forms"); 085 forms.add(formParams); 086 } 087 } 088 } 089 catch (FormsException e) 090 { 091 getLogger().error("Error getting forms for a content.", e); 092 } 093 } 094 095 return results; 096 } 097}