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.plugins.forms.data;
017
018import java.io.IOException;
019import java.io.InputStream;
020import java.sql.Connection;
021import java.sql.PreparedStatement;
022import java.sql.ResultSet;
023import java.sql.SQLException;
024
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.cocoon.ProcessingException;
027import org.apache.cocoon.reading.ServiceableReader;
028import org.apache.commons.lang.StringUtils;
029import org.apache.excalibur.source.SourceUtil;
030import org.apache.tika.io.IOUtils;
031import org.xml.sax.SAXException;
032
033import org.ametys.core.datasource.ConnectionHelper;
034import org.ametys.core.datasource.dbtype.SQLDatabaseTypeExtensionPoint;
035import org.ametys.plugins.forms.table.FormTableManager;
036import org.ametys.runtime.config.Config;
037
038/**
039 * Reads a BLOB value of a form entry.
040 */
041public class FormEntryFileReader extends ServiceableReader
042{
043    private SQLDatabaseTypeExtensionPoint _sqlDatabaseTypeExtensionPoint;
044
045    private SQLDatabaseTypeExtensionPoint getSQLDatabaseTypeExtensionPoint()
046    {
047        if (_sqlDatabaseTypeExtensionPoint == null)
048        {
049            try
050            {
051                _sqlDatabaseTypeExtensionPoint = (SQLDatabaseTypeExtensionPoint) manager.lookup(SQLDatabaseTypeExtensionPoint.ROLE);
052            }
053            catch (ServiceException e)
054            {
055                throw new RuntimeException(e);
056            }
057        }
058        return _sqlDatabaseTypeExtensionPoint;
059    }
060    
061    @Override
062    public void generate() throws IOException, SAXException, ProcessingException
063    {
064        String siteName = parameters.getParameter("site", "");
065        String formId = parameters.getParameter("form-id", "");
066        int entryId = parameters.getParameterAsInteger("entry-id", Integer.MIN_VALUE);
067        String fieldId = parameters.getParameter("field-id", "");
068        
069        if (StringUtils.isEmpty(siteName) || StringUtils.isEmpty(formId)
070            || entryId == Integer.MIN_VALUE || StringUtils.isEmpty(fieldId))
071        {
072            throw new IllegalArgumentException("Site name, form id, entry id and field id must be provided.");
073        }
074        
075        String tableName = FormTableManager.TABLE_PREFIX + formId;
076        
077        Connection connection = null;
078        PreparedStatement stmt = null;
079        ResultSet rs = null;
080        InputStream is = null;
081        
082        
083        try
084        {
085            String dataSourceId = Config.getInstance().getValue(FormTableManager.FORMS_POOL_CONFIG_PARAM);
086            connection = ConnectionHelper.getConnection(dataSourceId);
087            
088            String dbType = ConnectionHelper.getDatabaseType(connection);
089            String sql = "SELECT " + getSQLDatabaseTypeExtensionPoint().languageEscapeTableName(dbType, fieldId) + " FROM " + getSQLDatabaseTypeExtensionPoint().languageEscapeTableName(dbType, tableName) + " WHERE id = ?";
090            
091            stmt = connection.prepareStatement(sql);
092            
093            stmt.setInt(1, entryId);
094            
095            // Execute the query.
096            rs = stmt.executeQuery();
097            
098            // Extract the result.
099            if (rs.next())
100            {
101                is = _sqlDatabaseTypeExtensionPoint.getBlob(dbType, rs, 1);
102                
103                if (is != null)
104                {
105                    SourceUtil.copy(is, out);
106                }
107            }
108        }
109        catch (SQLException e)
110        {
111            getLogger().error("Error reading a form entry blob." + tableName, e);
112            throw new ProcessingException("Error reading a form entry blob.", e);
113        }
114        finally
115        {
116            IOUtils.closeQuietly(is);
117            
118            ConnectionHelper.cleanup(rs);
119            ConnectionHelper.cleanup(stmt);
120            ConnectionHelper.cleanup(connection);
121        }
122    }
123}