package de.taimos.dvalin.interconnect.demo;

/*
 * #%L
 * Dvalin interconnect demo project
 * %%
 * Copyright (C) 2016 Taimos GmbH
 * %%
 * 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.
 * #L%
 */

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import de.taimos.daemon.spring.annotations.ProdComponent;
import de.taimos.dvalin.interconnect.core.spring.DaemonProxyFactory;
import de.taimos.dvalin.interconnect.demo.api.IUserService;
import de.taimos.dvalin.interconnect.demo.api.UserError;
import de.taimos.dvalin.interconnect.demo.model.UserIVO_v1;
import de.taimos.dvalin.interconnect.demo.model.requests.CreateUserIVO_v1;
import de.taimos.dvalin.interconnect.demo.model.requests.FindUserByIdIVO_v1;
import de.taimos.dvalin.interconnect.demo.model.requests.FindUserIVO_v1;
import de.taimos.dvalin.interconnect.model.service.DaemonError;

@ProdComponent
public class Client {

    @Autowired
    private DaemonProxyFactory proxy;

    @Scheduled(initialDelay = 1000, fixedRate = 60000)
    public void init() {
        IUserService svc = proxy.create(IUserService.class);

        try {
            System.out.println("Users:");
            for (UserIVO_v1 user : svc.findUsers(new FindUserIVO_v1.FindUserIVO_v1Builder().build()).getElements()) {
                System.out.println("  - " + user.getId() + " " + user.getName());
            }

            System.out.println();

            for (int i = 0; i < 5; i++) {
                String name = UUID.randomUUID().toString();
                UserIVO_v1.UserIVO_v1Builder b = new UserIVO_v1.UserIVO_v1Builder();
                b.withName(name);
                UserIVO_v1 user = b.build();
                System.out.println("Creating user: " + name);
                UserIVO_v1 cUser = svc.createUser(new CreateUserIVO_v1.CreateUserIVO_v1Builder().withValue(user).build());
                System.out.println("Created user: " + cUser.getName() + " with id " + cUser.getId());
            }

            System.out.println();

            System.out.println("Users:");
            for (UserIVO_v1 user : svc.findUsers(new FindUserIVO_v1.FindUserIVO_v1Builder().build()).getElements()) {
                System.out.println("  - " + user.getId() + " " + user.getName());
            }

            svc.findById(new FindUserByIdIVO_v1.FindUserByIdIVO_v1Builder().withId("1000").build());
        } catch (DaemonError daemonError) {
            if (daemonError.getNumber().equals(UserError.USER_NOT_FOUND)) {
                System.out.println("User does not exist");
            }

            System.out.println(daemonError.getNumber().toString());
            daemonError.printStackTrace();
        }

    }

}

@@ -310,17 +310,16 @@
 se at%0A *
- 
 %0A *     
@@ -364,17 +364,16 @@
 E-2.0%0A *
- 
 %0A * Unle
@@ -726,71 +726,8 @@
 D;%0A%0A
-import org.springframework.beans.factory.annotation.Autowired;%0A
 impo
@@ -887,33 +887,27 @@
 ore.
-spring.DaemonProxyFactory
+daemon.Interconnect
 ;%0Aim
@@ -1160,85 +1160,8 @@
 v1;%0A
-import de.taimos.dvalin.interconnect.demo.model.requests.FindUserByIdIVO_v1;%0A
 impo
@@ -1341,17 +1341,20 @@
    @
-Autowired
+Interconnect
 %0A   
@@ -1366,32 +1366,24 @@
 ate 
-DaemonProxyFactory proxy
+IUserService svc
 ;%0A%0A 
@@ -1465,70 +1465,8 @@
 ) %7B%0A
-        IUserService svc = proxy.create(IUserService.class);%0A%0A
     
@@ -1544,32 +1544,37 @@
 erIVO_v1 user : 
+this.
 svc.findUsers(ne
@@ -2135,16 +2135,21 @@
 cUser = 
+this.
 svc.crea
@@ -2457,16 +2457,21 @@
  user : 
+this.
 svc.find
@@ -2634,32 +2634,34 @@
 %0A            %7D%0A%0A
+//
             svc.

use annotation instead of proxy factory