Package

Source Code of Controller

/*
* Copyright 2004-2005 The Apache Software Foundation.
*
* 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.
*
* $Header:$
*/
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.beehive.controls.api.bean.Control;
import org.apache.beehive.netui.pageflow.PageFlowController;
import org.apache.beehive.netui.pageflow.Forward;
import org.apache.beehive.netui.pageflow.annotations.Jpf;
import org.apache.beehive.samples.petstore.controls.CatalogControl;
import org.apache.beehive.samples.petstore.controls.data.DBProperties;
import org.apache.beehive.samples.petstore.controls.exceptions.DataStoreException;

import webappRoot.SharedFlow;

@Jpf.Controller(
    sharedFlowRefs={
        @Jpf.SharedFlowRef(name="rootSharedFlow", type=webappRoot.SharedFlow.class)
    }
)
public class Controller
    extends PageFlowController
{
    private static final String SQL_FILE = "sql/initDB.sql";

    @Jpf.SharedFlowField(name="rootSharedFlow")
    private SharedFlow _sharedFlow;

    @Control()
    private CatalogControl _catalogControl;
 
    @Jpf.Action(
        forwards = {
            @Jpf.Forward(name = "shop", path = "/shop/Controller.jpf"),
            @Jpf.Forward(name = "index", path = "/index.jsp")
        }
    )
    protected Forward begin()
    {
        if (_sharedFlow.isUserLoggedIn())
            return new Forward("shop");
        else
      return new Forward("index");
    }

   @Jpf.Action(
            forwards = {
                @Jpf.Forward(name = "success", path = "index.jsp")
            }
        )
        public Forward stopDB() throws Exception
        {
         try {
        java.sql.DriverManager.getConnection("jdbc:derby:" + DBProperties.dbLocation + ";shutdown=true");
         } catch (Exception e) {}
      getRequest().setAttribute("message", "DB Stopped.");
            return new Forward("success");
        }
    
    @Jpf.Action(
          forwards = {
              @Jpf.Forward(name = "index", path = "index.jsp")
          }
      )
      protected Forward initDB() throws Exception
      {
      List<String> dbInit = getSqlStatements(SQL_FILE);

      for(String sql : dbInit)
      {
        try {
          _catalogControl.initDB(sql);
        } catch (DataStoreException e) {
          // Ignore errors on the drop action since the table may not exist
          if (!sql.startsWith("drop"))
            throw e;
        }
      }
      getRequest().setAttribute("message", "DB Initialized.");

      return new Forward("index");
      }

     private List<String> getSqlStatements(String sqlFile) throws Exception
   {
         InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(sqlFile);
         BufferedReader fs = new BufferedReader(new InputStreamReader(is));
        List<String> statements = new ArrayList<String>();
        try {
            String line = null;
            while((line = fs.readLine()) != null)
      {
        // Ignore comments and empty lines
        if (line.endsWith(";") && !(line.startsWith("--")))
                    statements.add(line.replace(';', ' '));
            }
        } catch(IOException io) {
            throw new RuntimeException("Unable to read input stream: " + sqlFile);
        } finally {
            try {
                if(fs != null) fs.close();
            } catch(Exception ignore) {}
    }
        for(String s : statements) {
            System.out.println(s + "\n");
        }

        return statements;
   }
}
TOP

Related Classes of Controller

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.