by.bigvova.backend.MyBackendBean.java Source code

Java tutorial

Introduction

Here is the source code for by.bigvova.backend.MyBackendBean.java

Source

/*
 * Copyright 2015 The original authors
 *
 * 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 by.bigvova.backend;

import by.bigvova.backend.controllers.MealController;
import by.bigvova.logging.LoggerWrapper;
import com.vaadin.ui.Notification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * Implementation of {@link by.bigvova.backend.MyBackend}.
 *
 * @author Petter Holmstrm (petter@vaadin.com)
 */
@Service
public class MyBackendBean implements MyBackend {

    @Autowired
    private ApplicationContext ctx;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private MealController controller;

    @Override
    public String adminOnlyEcho(String s) {
        return "admin:" + s;
    }

    @Override
    public String echo(String s) {
        return s;
    }

    @Override
    public void populateDB() {
        try {
            ScriptUtils.executeSqlScript(dataSource.getConnection(),
                    ctx.getResource("classpath:db/initAndPopulateBigDB.sql"));
            controller.cacheEvict();
            Notification.show("Done!!!");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}