001/* 002 * (C) Copyright 2006-20011 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 */ 018 019package org.nuxeo.ecm.platform.reporting.seam; 020 021import static org.jboss.seam.annotations.Install.FRAMEWORK; 022import static org.nuxeo.ecm.platform.ui.web.component.file.InputFileMimetypeValidator.MIMETYPE_AUTHORIZED_EXTENSIONS_MESSAGE_ID; 023 024import java.io.Serializable; 025import java.util.ArrayList; 026import java.util.List; 027 028import javax.faces.component.UIComponent; 029import javax.faces.context.FacesContext; 030import javax.faces.validator.ValidatorException; 031 032import org.apache.commons.logging.Log; 033import org.apache.commons.logging.LogFactory; 034import org.jboss.seam.ScopeType; 035import org.jboss.seam.annotations.Factory; 036import org.jboss.seam.annotations.In; 037import org.jboss.seam.annotations.Install; 038import org.jboss.seam.annotations.Name; 039import org.jboss.seam.annotations.Scope; 040import org.nuxeo.ecm.core.api.CoreSession; 041import org.nuxeo.ecm.core.api.DocumentModel; 042import org.nuxeo.ecm.core.api.DocumentNotFoundException; 043import org.nuxeo.ecm.core.api.IdRef; 044import org.nuxeo.ecm.core.api.PathRef; 045import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner; 046import org.nuxeo.ecm.core.api.security.ACE; 047import org.nuxeo.ecm.core.api.security.ACL; 048import org.nuxeo.ecm.core.api.security.ACP; 049import org.nuxeo.ecm.core.api.security.SecurityConstants; 050import org.nuxeo.ecm.core.api.security.impl.ACLImpl; 051import org.nuxeo.ecm.core.api.security.impl.ACPImpl; 052import org.nuxeo.ecm.platform.reporting.api.Constants; 053import org.nuxeo.ecm.platform.reporting.api.ReportModel; 054import org.nuxeo.ecm.platform.reporting.api.ReportService; 055import org.nuxeo.ecm.platform.ui.web.component.file.InputFileChoice; 056import org.nuxeo.ecm.platform.ui.web.component.file.InputFileInfo; 057import org.nuxeo.ecm.platform.usermanager.UserManager; 058import org.nuxeo.ecm.webapp.contentbrowser.DocumentActions; 059import org.nuxeo.runtime.api.Framework; 060 061import com.sun.faces.util.MessageFactory; 062 063/** 064 * Seam Bean used to manage Edit form 065 * 066 * @author Tiry (tdelprat@nuxeo.com) 067 */ 068@Name("reportActions") 069@Scope(ScopeType.PAGE) 070@Install(precedence = FRAMEWORK) 071public class ReportActions implements Serializable { 072 073 private static final long serialVersionUID = -1155863420157051403L; 074 075 protected static final Log log = LogFactory.getLog(ReportActions.class); 076 077 @In(create = true) 078 protected transient CoreSession documentManager; 079 080 @In(create = true) 081 protected transient DocumentActions documentActions; 082 083 @In(create = true) 084 protected transient UserManager userManager; 085 086 protected String reportsContainerPath = null; 087 088 protected DocumentModel newReportModel = null; 089 090 protected boolean showForm = false; 091 092 @Factory(value = "reportModels", scope = ScopeType.EVENT, autoCreate = true) 093 public List<ReportModel> getAvailableModels() { 094 ReportService rs = Framework.getLocalService(ReportService.class); 095 return rs.getReportAvailableModels(documentManager); 096 } 097 098 public ReportModel getReportModel(String docId) { 099 try { 100 DocumentModel reportModelDoc = documentManager.getDocument(new IdRef(docId)); 101 return reportModelDoc.getAdapter(ReportModel.class); 102 } catch (DocumentNotFoundException e) { 103 return null; 104 } 105 } 106 107 public DocumentModel getBareReportModel() { 108 return documentManager.createDocumentModel(Constants.BIRT_REPORT_MODEL_TYPE); 109 } 110 111 public DocumentModel getNewReportModel() { 112 if (newReportModel == null) { 113 newReportModel = getBareReportModel(); 114 } 115 return newReportModel; 116 } 117 118 public String saveDocument() { 119 createReportsModelContainerIfNeeded(); 120 String view = documentActions.saveDocument(newReportModel); 121 resetDocument(); 122 toggleForm(); 123 return view; 124 } 125 126 protected void createReportsModelContainerIfNeeded() { 127 String path = getReportModelsContainerPath(); 128 if (!documentManager.exists(new PathRef(path))) { 129 createReportsModelContainer(path); 130 } 131 } 132 133 protected void createReportsModelContainer(String path) { 134 new UnrestrictedReportModelsContainerCreator(documentManager, path).runUnrestricted(); 135 } 136 137 protected void resetDocument() { 138 newReportModel = null; 139 } 140 141 public String getReportModelsContainerPath() { 142 if (reportsContainerPath == null) { 143 ReportService reportService = Framework.getService(ReportService.class); 144 reportsContainerPath = reportService.getReportModelsContainer(); 145 } 146 return reportsContainerPath; 147 } 148 149 public boolean isShowForm() { 150 return showForm; 151 } 152 153 public void toggleForm() { 154 showForm = !showForm; 155 } 156 157 public void toggleAndReset() { 158 toggleForm(); 159 resetDocument(); 160 } 161 162 public void validateReportExtension(FacesContext context, UIComponent component, Object value) { 163 if (value instanceof InputFileInfo) { 164 InputFileInfo info = (InputFileInfo) value; 165 String choice = info.getConvertedChoice(); 166 if (!InputFileChoice.isUploadOrKeepTemp(choice)) { 167 return; 168 } 169 String filename = info.getConvertedFilename(); 170 if (filename != null) { 171 if (!filename.endsWith(".rptdesign")) { 172 throw new ValidatorException(MessageFactory.getMessage(context, 173 MIMETYPE_AUTHORIZED_EXTENSIONS_MESSAGE_ID, ".rptdesign")); 174 } 175 } 176 } 177 } 178 179 public class UnrestrictedReportModelsContainerCreator extends UnrestrictedSessionRunner { 180 181 protected String reportModelsContainerPath; 182 183 protected UnrestrictedReportModelsContainerCreator(CoreSession session, String reportModelsContainerPath) { 184 super(session); 185 this.reportModelsContainerPath = reportModelsContainerPath; 186 } 187 188 @Override 189 public void run() { 190 if (!session.exists(new PathRef(reportModelsContainerPath))) { 191 DocumentModel doc = session.createDocumentModel(session.getRootDocument().getPathAsString(), 192 reportModelsContainerPath.substring(1), Constants.BIRT_REPORT_MODELS_ROOT_TYPE); 193 doc.setPropertyValue("dc:title", "Report Models"); 194 doc = session.createDocument(doc); 195 196 ACP acp = new ACPImpl(); 197 ACL acl = new ACLImpl(); 198 for (String administratorGroup : userManager.getAdministratorsGroups()) { 199 ACE ace = new ACE(administratorGroup, SecurityConstants.EVERYTHING, true); 200 acl.add(ace); 201 } 202 acp.addACL(acl); 203 doc.setACP(acp, true); 204 session.save(); 205 } 206 } 207 } 208 209}