<repo_name>valeriaedw/Examen2-Comp<file_sep>/src/main/java/com/cenfotec/examen/service/workshop/WorkShopServiceImpl.java
package com.cenfotec.examen.service.workshop;

import com.cenfotec.examen.domain.WorkShop;
import com.cenfotec.examen.repo.WorkShopRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class WorkShopServiceImpl implements WorkShopService{
    @Autowired
    WorkShopRepository repo;
    @Override
    public void save(WorkShop workshop) {
        repo.save(workshop);
    }

    @Override
    public List<WorkShop> getAll() {
        return repo.findAll();
    }

    @Override
    public Optional<WorkShop> get(Long id) {
        return repo.findById(id);
    }

    @Override
    public List<WorkShop> find(String name) {
        return repo.findByNameContaining(name);
    }

    @Override
    public List<WorkShop> findByCategory(String category) {
        return repo.findByCategory(category);
    }

    @Override
    public List<WorkShop> findByAutor(String autor) {
        return repo.findByAutor(autor);
    }

    @Override
    public List<WorkShop> findByKeyword(String keyword) {
        return repo.findByKeyword(keyword);
    }


}
<file_sep>/src/main/java/com/cenfotec/examen/service/workshop/WorkShopService.java
package com.cenfotec.examen.service.workshop;


import com.cenfotec.examen.domain.WorkShop;


import java.util.List;
import java.util.Optional;

public interface WorkShopService {
    public void save(WorkShop workshop);
    public List<WorkShop> getAll();
    public Optional<WorkShop> get(Long id);
    public List<WorkShop> find(String name);
    public List<WorkShop> findByCategory(String category);
    public List<WorkShop> findByAutor(String autor);
    public List<WorkShop> findByKeyword( String keyword);

}
<file_sep>/src/main/java/com/cenfotec/examen/controller/WordController.java
package com.cenfotec.examen.controller;

import com.cenfotec.examen.domain.WorkShop;
import com.cenfotec.examen.service.workshop.WorkShopService;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Optional;

@RestController
@RequestMapping("/report/{id}")
public class WordController{
    @Autowired
    WorkShopService workShopService;

    @GetMapping
    public void getGeneratedDocument(HttpServletResponse response, @PathVariable long id) throws IOException {
        response.setHeader("Content-disposition","attachment; filename=test.docx");

        Optional<WorkShop> workShopData = workShopService.get(id);
        if (workShopData.isPresent()){
            WorkShop workshop = workShopData.get();
            XWPFDocument document = new XWPFDocument();

            XWPFParagraph body = document.createParagraph();
            XWPFRun bodyRun = body.createRun();
            bodyRun.setText("Taller: "+ workshop.getName()+ "\n"+"Autor: "+ workshop.getAutor()+"\nObjetivo: "+workshop.getObjetivo()+
                    "\nCategoria: "+ workshop.getCategory().getName()+"\nPalabras clave: "+workshop.getKeyword1() + " " +workshop.getKeyword2()+
                    " "+workshop.getKeyword3() + "\nActividades: " + workshop.getActivity().toString());

            document.write(response.getOutputStream());



        }

    }


}
<file_sep>/src/main/java/com/cenfotec/examen/domain/WorkShop.java
package com.cenfotec.examen.domain;

import javax.persistence.*;
import java.time.LocalTime;
import java.util.Set;

@Entity
public class WorkShop {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String name;
    private String autor;
    private String objetivo;
    private String keyword1;
    private String keyword2;
    private String keyword3;
    private LocalTime duracionTotal = LocalTime.of(0,0);

   @ManyToOne
   @JoinColumn(name="cart_id", nullable = false)
   private Category category;

   @OneToMany(fetch=FetchType.LAZY, mappedBy="workshop")
   private Set<Activity> activity;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAutor() {
        return autor;
    }

    public void setAutor(String autor) {
        this.autor = autor;
    }

    public String getObjetivo() {
        return objetivo;
    }

    public void setObjetivo(String objetivo) {
        this.objetivo = objetivo;
    }

    public String getKeyword1() {
        return keyword1;
    }

    public void setKeyword1(String keyword1) {
        this.keyword1 = keyword1;
    }

    public String getKeyword2() {
        return keyword2;
    }

    public void setKeyword2(String keyword2) {
        this.keyword2 = keyword2;
    }

    public String getKeyword3() {
        return keyword3;
    }

    public void setKeyword3(String keyword3) {
        this.keyword3 = keyword3;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public Set<Activity> getActivity() {
        return activity;
    }

    public void setActivity(Set<Activity> activity) {
        this.activity = activity;
    }

    public LocalTime getDuracionTotal() {
        return duracionTotal;
    }

    public void setDuracionTotal(LocalTime duracionTotal) {
        this.duracionTotal = duracionTotal;
    }
}
<file_sep>/src/main/java/com/cenfotec/examen/service/activity/ActivityServiceImpl.java
package com.cenfotec.examen.service.activity;

import com.cenfotec.examen.domain.Activity;
import com.cenfotec.examen.repo.ActivityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    ActivityRepository repo;

    @Override
    public void save(Activity activity) {
        repo.save(activity);
    }

    @Override
    public List<Activity> getAll() {
        return repo.findAll();
    }

    @Override
    public Optional<Activity> get(Long id) {
        return repo.findById(id);
    }

    @Override
    public List<Activity> find(String name) {
        return repo.findByNameContaining(name);
    }
}
<file_sep>/src/main/java/com/cenfotec/examen/controller/ActivityController.java
package com.cenfotec.examen.controller;

import com.cenfotec.examen.domain.Activity;
import com.cenfotec.examen.service.activity.ActivityService;
import com.cenfotec.examen.service.workshop.WorkShopService;
import com.cenfotec.examen.domain.WorkShop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.time.LocalTime;
import java.util.Optional;

@Controller
public class ActivityController {
    @Autowired
    ActivityService activityService;
    @Autowired
    WorkShopService workShopService;

    @RequestMapping(value="/registra_actividades/{id}")
    public String recoverForAddActivity(Model model, @PathVariable long id) {
        Optional<WorkShop> workShop = workShopService.get(id);
        Activity newActivity = new Activity();
        if (workShop.isPresent()) {
            newActivity.setWorkshop(workShop.get());
            model.addAttribute("workshop",workShop.get());
            model.addAttribute("activity",newActivity);
            return "registra_actividades";
        }
        return "notfound";
    }

    @RequestMapping(value="/registra_actividades/{id}", method = RequestMethod.POST)
    public String saveActivity(Activity activity, Model model, @PathVariable long id) {
        Optional<WorkShop> workShop = workShopService.get(id);
        if (workShop.isPresent()) {
            LocalTime total = sumaDuracion(workShop.get().getDuracionTotal(), activity.getHour());

            workShop.get().setDuracionTotal(total);
            workShopService.save(workShop.get());

            activity.setWorkshop(workShop.get());
            activityService.save(activity);
            return "index";
        }
        return "notfound";
    }

    private LocalTime sumaDuracion(LocalTime duracionActual, LocalTime newTime){
        Integer actualHour = duracionActual.getHour();
        Integer actualMinute = duracionActual.getMinute();
        Integer newHour = newTime.getHour();
        Integer newMinute = newTime.getMinute();

        Integer hour = actualHour += newHour;
        Integer minute = actualMinute += newMinute;

        return LocalTime.of(hour,minute);
    }
}
