<file_sep>/*
 * (c) Copyright 2014 Swisscom AG
 * All Rights Reserved.
 */
package com.swisscom.cloud.demo.spring.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.swisscom.cloud.demo.spring.model.Person;


/**
 * @author <NAME>
 * @since 24.02.2014
 */
@Transactional
@Component
public class PersonRepositoryImpl implements PersonRepository {

    @PersistenceContext
    private EntityManager em;

    @Override
    public List<Person> findAll() {
        Query q = em.createQuery("SELECT p FROM Person p");
        @SuppressWarnings("unchecked")
        List<Person> result = q.getResultList();
        return result;
    }

    @Override
    public Person findOne(String id) {
        Query q = em.createQuery("SELECT p FROM Person p WHERE p.id = :id");
        q.setParameter("id", id);
        return (Person) q.getSingleResult();
    }

    @Override
    public <S extends Person> S save(Person person) {
        em.merge(person);
        @SuppressWarnings("unchecked")
        S result = (S) person;
        return result;
    }

    @Override
    public void delete(Person person) {
        Person loaded = findOne(person.getId());
        if (loaded != null) {
            em.remove(loaded);
        }
    }

}
<file_sep>/*
 * (c) Copyright 2014 Swisscom AG
 * All Rights Reserved.
 */
package com.swisscom.cloud.demo.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;

/**
 * @author <NAME>
 * @since 24.02.2014
 */
@Controller
public class HomeController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public View home() {
        return new RedirectView("/person", true);
    }
    
}
<file_sep>cf-spring-demo
===========

Simple Annotation Based Spring Demo Web-Application for Cloud Foundry.

Dependencies
------------

-    Running mysql database
-    Maven
-    Spring
-    Hibernate

Usage
-----

Login to the Swisscom Application Cloud and switch to your Org and Space, then

    mvn clean package
    cf create-service mariadb small maria-d
    cf push 


Configuration
-------------

Local

If you want to connect to a mysql database which does not run on localhost or
your connection settings differ from the provided defaults, then you want to
modify the file `src/main/resources/application.properties`.

Cloud

In the class `SpringDemoWebApplication` it is assumed that exactly one
service instance is bound to this application (see JavaDoc comments). This
service instance is assumed to be mysql or mariadb. If these assumptions
are not valid, you want to modify the code.

Feedback
--------

Most welcome. Create pull-requests or contact me via email.
<file_sep>/*
 * (c) Copyright 2014 Swisscom AG
 * All Rights Reserved.
 */
package com.swisscom.cloud.demo.spring.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;

import com.swisscom.cloud.demo.spring.model.Person;
import com.swisscom.cloud.demo.spring.service.PersonService;

/**
 * @author <NAME>
 * @since 24.02.2014
 */
@Controller
public class PersonController {

    @Autowired
    private PersonService svc;

    @RequestMapping(value = "/person", method = RequestMethod.GET)
    public String getPersonList(ModelMap model) {
        model.addAttribute("personList", svc.listPerson());
        return "person-list";
    }

    @RequestMapping(value = "/person/save", method = RequestMethod.POST)
    public View createPerson(@ModelAttribute Person person, ModelMap model) {
        if (StringUtils.hasText(person.getId())) {
            svc.updatePerson(person);
        } else {
            svc.addPerson(person);
        }
        return new RedirectView("/person", true);
    }

    @RequestMapping(value = "/person/delete", method = RequestMethod.GET)
    public View deletePerson(@ModelAttribute Person person, ModelMap model) {
        svc.deletePerson(person);
        return new RedirectView("/person", true);
    }

    @RequestMapping(value = "/crash", method = RequestMethod.GET)
    public void crashApp() {
            // crash app
            System.out.println("crashing...");
            crashApplication();
    }

    public void crashApplication() {
        Object[] o = null;

        while (true) {
            o = new Object[] {o};
        }
    }

}
<file_sep>$(document).foundation();

/*
 * Init
 */
$(document).ready(function(){
    loadSoundFile();
});

/*
 * Not yet implemented
 */
function notYetImplemented() {
    $("#soon").show();
}

/*
 * Load images and activate Slick
 */
function readyToPlay() {

    //$.getJSON("http://api.flickr.com/services/feeds/groups_pool.gne?id=998875@N22âŒ©=en-us&amp;format=json&amp;jsoncallback=?", displayImages);
    $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
    {
        tags: "Swisscom",
        format: "json"
    },
    displayImages);
    playSound();
}

function displayImages(data) {

    $.each(data.items, function(i,item){
        $("<img/>").attr("src", item.media.m).prependTo($("<div/>").attr("class", "hide").prependTo("#loadedImages"));
        if ( i == 10 ) return false;
    });
    activateSlick();
}

function activateSlick() {
    $('.slick-container').slick({
        autoplay: true,
        autoplaySpeed: 1000,
        arrows: false,
        fade: true//,
        //centerMode: true
    });
}


/*
 * Sound
 */
var soundfile;
function loadSoundFile() {
    soundfile = "http://cf-demo.beta.swisscloud.io/audio/wenn.mp3";
}

function playSound() {

    //soundfile = "http://cf-demo.beta.swisscloud.io/audio/wenn.mp3";
    el = $("#soundElement");
    if (el.mp3) {
        if(el.mp3.paused) el.mp3.play();
        else el.mp3.pause();
    } else {
        el.mp3 = new Audio(soundfile);
        el.mp3.play();
    }
}

/*!
 * jQuery Sticky Footer 1.1
 * <NAME>
 * http://tangerineindustries.com
 *
 * Released under the MIT license
 *
 * Copyright 2013 <NAME>.
 *
 * Date: Thu Jan 22 2013 13:34:00 GMT-0630 (Eastern Daylight Time)
 * Modification for jquery 1.9+ Tue May 7 2013
 * Modification for non-jquery, removed all, now classic JS Wed Jun 12 2013
 */

window.onload = function() {
    stickyFooter();

    //you can either uncomment and allow the setInterval to auto correct the footer
    //or call stickyFooter() if you have major DOM changes
    //setInterval(checkForDOMChange, 1000);
};

//check for changes to the DOM
function checkForDOMChange() {
    stickyFooter();
}

//check for resize event if not IE 9 or greater
window.onresize = function() {
    stickyFooter();
};

//lets get the marginTop for the <footer>
function getCSS(element, property) {

    var elem = document.getElementsByTagName(element)[0];
    var css = null;

    if (elem.currentStyle) {
        css = elem.currentStyle[property];

    } else if (window.getComputedStyle) {
        css = document.defaultView.getComputedStyle(elem, null).
            getPropertyValue(property);
    }

    return css;

}

function stickyFooter() {

    if (document.getElementsByTagName("footer")[0].getAttribute("style") !== null) {
        document.getElementsByTagName("footer")[0].removeAttribute("style");
    }

    if (window.innerHeight != document.body.offsetHeight) {
        var offset = window.innerHeight - document.body.offsetHeight;
        var current = getCSS("footer", "margin-top");

        if (isNaN(current) === true) {
            document.getElementsByTagName("footer")[0].setAttribute("style","margin-top:0px;");
            current = 0;
        } else {
            current = parseInt(current);
        }

        if (current+offset > parseInt(getCSS("footer", "margin-top"))) {
            document.getElementsByTagName("footer")[0].setAttribute("style","margin-top:"+(current+offset)+"px;");
        }
    }
}

/*
 ! end sticky footer
 */