/*
 * Copyright 2016 the original author or 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 se.omegapoint.facepalm.client.config;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import se.omegapoint.facepalm.client.adapters.ImageAdapter;
import se.omegapoint.facepalm.client.models.ImageUpload;

import java.io.BufferedInputStream;
import java.io.InputStream;

import static org.apache.commons.lang3.Validate.notNull;

/**
 * This class bootstraps some data which cannot be easily added via import.sql
 */
@Service
public class Bootstrapper implements InitializingBean {

    private static final Logger LOGGER = LoggerFactory.getLogger(Bootstrapper.class);

    private final ImageAdapter imageAdapter;
    private final Environment env;

    @Autowired
    public Bootstrapper(final ImageAdapter imageAdapter, final Environment env) {
        this.imageAdapter = notNull(imageAdapter);
        this.env = notNull(env);
    }

    @Override
    @Transactional
    public void afterPropertiesSet() throws Exception {
        if (Boolean.valueOf(env.getProperty("bootstrap.data"))) {
            LOGGER.debug("Started bootstrapping of image posts");
            uploadImageFileWithTitle("He is right behind me, isn't he?", "samples/a.jpg");
            uploadImageFileWithTitle("I cant believe it doesn't work this way", "samples/b.jpg");
            uploadImageFileWithTitle("Mhmm.. pie", "samples/c.jpg");
            LOGGER.debug("Done with bootstrapping");
        }
    }

    private void uploadImageFileWithTitle(final String title, final String filePath) {
        try {
            final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
            imageAdapter.addImage(new ImageUpload(title, IOUtils.toByteArray(new BufferedInputStream(is))));
        } catch (Exception e) {
            LOGGER.error("Unable to create image with title " + title);
        }
    }
}

@@ -656,16 +656,56 @@
 onfig;%0A%0A
+import org.apache.commons.io.FileUtils;%0A
 import o
@@ -730,24 +730,24 @@
 io.IOUtils;%0A
-
 import org.s
@@ -1228,16 +1228,65 @@
 Stream;%0A
+import java.io.File;%0Aimport java.io.IOException;%0A
 import j
@@ -2410,24 +2410,64 @@
 );%0A        %7D
+%0A%0A        createDocumentsIfNotPresent();
 %0A    %7D%0A%0A    
@@ -2813,24 +2813,24 @@
 eption e) %7B%0A
-
             
@@ -2904,11 +2904,1228 @@
  %7D%0A    %7D
+%0A%0A    private void createDocumentsIfNotPresent() %7B%0A        File documentsFolder = new File(%22./docsds%22);%0A        if (!documentsFolder.isDirectory()) %7B%0A            LOGGER.info(%22Creating missing documents folder with sample text files...%22);%0A            documentsFolder.mkdir();%0A            createDocument(documentsFolder, %22security.txt%22, %22This is our security policy! Quite empty though...%22);%0A            createDocument(documentsFolder, %22advertising.txt%22, %22You are allowed to advertise by emailing us.%22);%0A            createDocument(documentsFolder, %22general.txt%22, %22Welcome to Facepalm! Find friends or upload an image.%22);%0A            createDocument(documentsFolder, %22copyright.txt%22, %22Take what you want and run with it!%22);%0A        %7D%0A    %7D%0A%0A    private void createDocument(final File documentsFolder, final String filename, final String content) %7B%0A        final File security = new File(documentsFolder.getPath() + %22/%22 + filename);%0A        try %7B%0A            FileUtils.writeStringToFile(security, content);%0A        %7D catch (IOException e) %7B%0A            LOGGER.error(%22Could not generate sample text file: %22 + e.getMessage());%0A            throw new IllegalStateException(%22Could not create sample documents%22);%0A        %7D%0A    %7D
 %0A%7D%0A

Create sample documents if not present in same working directory