package org.udg.pds.simpleapp_javaee.util;


import io.minio.MinioClient;

import javax.annotation.PostConstruct;
import javax.ejb.DependsOn;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;
import org.apache.log4j.Logger;
import org.udg.pds.simpleapp_javaee.model.User;
import org.udg.pds.simpleapp_javaee.service.TaskService;
import org.udg.pds.simpleapp_javaee.service.UserService;

import java.util.Date;

@Singleton
@Startup
@DependsOn({"UserService", "TaskService"})
public class Global {
    private MinioClient minioClient;
    private String minioBucket;
    private String BASE_URL;

    @Inject
    private Logger logger;

    @EJB
    UserService userService;

    @EJB
    TaskService taskService;

    @PostConstruct
    void init() {
        String minioURL = null;
        String minioAccessKey = null;
        String minioSecretKey = null;

        try {
            minioURL = System.getProperty("swarm.project.minio.url");
            minioAccessKey = System.getProperty("swarm.project.minio.access-key");
            minioSecretKey = System.getProperty("swarm.project.minio.secret-key");
            minioClient = new MinioClient(minioURL, minioAccessKey, minioSecretKey);
            minioBucket = System.getProperty("swarm.project.minio.bucket");
        } catch (Exception e) {
            logger.warn("Cannot initialize minio service with url:" + minioURL + ", access-key:" + minioAccessKey + ", secret-key:" + minioSecretKey);
        }

        if (minioBucket == null) {
            logger.warn("Cannot initialize minio bucket: " + minioBucket);
            minioClient = null;
        }

        if (System.getProperty("swarm.project.base-url") != null)
            BASE_URL = System.getProperty("swarm.project.base-url");
        else {
            String port = System.getProperty("swarm.http.port") != null ? System.getProperty("swarm.http.port") : "8080";
            BASE_URL = "http://localhost:" + port;
        }

        initData();
    }

  private void initData() {
    User u = userService.register("usuari", "usuari@hotmail.com", "123456");
    taskService.addTask("Una tasca", u.getId(), new Date(), new Date());
  }

  public MinioClient getMinioClient() {
        return minioClient;
    }

    public String getBaseURL() { return BASE_URL; }
}

@@ -313,20 +313,171 @@
 e.model.
-User
+Tag;%0Aimport org.udg.pds.simpleapp_javaee.model.Task;%0Aimport org.udg.pds.simpleapp_javaee.model.User;%0Aimport org.udg.pds.simpleapp_javaee.service.TagService
 ;%0Aimport
@@ -585,16 +585,44 @@
 rvice;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -657,16 +657,18 @@
 Startup%0A
+//
 @Depends
@@ -702,16 +702,16 @@
 vice%22%7D)%0A
-
 public c
@@ -907,24 +907,24 @@
 ;%0A%0A    @EJB%0A
-
     TaskServ
@@ -937,24 +937,61 @@
 skService;%0A%0A
+    @EJB%0A    TagService tagService;%0A%0A
     @PostCon
@@ -2281,16 +2281,19 @@
   User u
+ser
  = userS
@@ -2354,16 +2354,28 @@
 6%22);%0A   
+ Task task =
  taskSer
@@ -2401,16 +2401,19 @@
 asca%22, u
+ser
 .getId()
@@ -2439,16 +2439,177 @@
 ate());%0A
+    Tag tag = tagService.addTag(%22ATag%22, %22Just a tag%22);%0A    taskService.addTagsToTask(user.getId(), task.getId(), new ArrayList%3CLong%3E() %7B%7B add(tag.getId()); %7D%7D);%0A
   %7D%0A%0A  p

Create a Tag and link it to a Task