/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.servlet.facilities;

import org.apache.log4j.Logger;


import org.bodington.servlet.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.math.BigInteger;


import org.bodington.server.*;
import org.bodington.server.events.*;
import org.bodington.server.realm.User;
import org.bodington.server.resources.Resource;
import org.bodington.server.resources.UploadedFileSummary;
import org.bodington.util.BodingtonURL;
import org.bodington.database.PrimaryKey;

public class MediaDocumentFacility extends org.bodington.servlet.facilities.Facility
	{
	
    private static Logger log = Logger.getLogger(MediaDocumentFacility.class);


    static final int MAX_FILENAME_LENGTH = 48;

	static final int MAX_FILE_SIZE = 10;
	static final int MAX_ZIP_SIZE  = 10;

	private static String file_store=null;
	private static String published=null;

    public boolean canCopy( Resource resource )
    {
        return true;
    }


	public void insert( Request req, PrintWriter out, String command, String insertname )
		throws ServletException, IOException
		{
		log.debug( Thread.currentThread().getName() + " MediaDocumentFacility insert()" );


		if ( command.equalsIgnoreCase( "startpage" ) )
			{
			if ( out==null )
			    return;
			    
			String page=null;
            page = getStartPage( req );
   			if ( page!=null )
				out.print( page );
			else
				out.print( "bs_template_fileindex.html" );
			return;
			}
		
		super.insert( req, out, command, insertname );
		}


/**
 * Checks whether text in introduction is an existing filename.
 * If the file exists, and isn't deleted, the URL for that file
 *  is returned (to be displayed in the bottom frame).
 * @param req
 * @return URL for existing, undeleted Uploaded File,
 *  or name of introduction template,
 *  or null if file is deleted or an exception is thrown.
 * @throws IOException
 */    

	public String getStartPage( Request req )
	throws IOException
	{
	    String page = "bs_template_intro.html";
	    try
	    {
	        UploadedFileSummary summary;
	        BuildingSession session;
	        
	        session = req.getBuildingSession();
	        
	        String intro = req.getResource().getIntroduction();
	        // See RT #1109914 (FCKEditor added <p>).
	        intro = intro.replaceAll("(?i)</?p>", "");
	        if (!intro.matches(".*[\\n\\t\\t ].*")) 
	        {
	            summary=session.getUploadedFileSession().getFileSummary( intro );
	            if ( summary!=null )
	            {
	                BodingtonURL bodURL = new BodingtonURL( req );
	                String fileURL = bodURL.getUploadedFileUrl( summary.getUploadedFileId(), true );
	                page = fileURL; // if it's null, it's better to output file index rather than bs_template_intro.html.
	            }
	        }
	    }
	    catch ( BuildingServerException bsex )
	    {
	        page = null;
	    }
	    
	    return page;
	}

	}

