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