// Path: src/main/java/org/fenixedu/learning/domain/degree/DegreeSiteListener.java
// public class DegreeSiteListener {
// 
//     @Atomic
//     public static Site create(Degree degree) {
//         LocalizedString name;
//     
//         if (degree.getPhdProgram() != null) {
//             name = new LocalizedString().with(Locale.getDefault(), degree.getPhdProgram().getPresentationName());
//         } else {
//             name = new LocalizedString().with(Locale.getDefault(), degree.getPresentationName());
//         }
//     
//         Site site = DegreeSiteBuilder.getInstance().create(name);
//     
//         RoleTemplate defaultTemplate = site.getDefaultRoleTemplate();
//         if (defaultTemplate != null ) {
//             Group group = Group.users(CoordinatorGroup.get(degree).getMembers());
//             site.getDefaultRoleTemplateRole().setGroup(group);
//         } else {
//             throw new DomainException("no.default.role");
//         }
//         
//         site.setDegree(degree);
//         site.setSlug(degree.getSigla());
//         
//         return site;
//     }
// 
// 
//     
//     
// }
// 
// Path: src/main/java/org/fenixedu/learning/domain/executionCourse/ExecutionCourseListener.java
// public class ExecutionCourseListener {
//     
// 
//     private static final Logger logger = LoggerFactory.getLogger(ExecutionCourseListener.class);
//     
//     public static Site create(ExecutionCourse executionCourse) {
//         Site newSite = ExecutionCourseSiteBuilder.getInstance().create(executionCourse);
//         
//         RoleTemplate defaultTemplate = newSite.getDefaultRoleTemplate();
//         if (defaultTemplate != null ) {
//             Role teacherRole =
//             new Role(defaultTemplate, executionCourse.getSite());
//     
//             Group group = teacherRole.getGroup();
//             for(Professorship pr : executionCourse.getProfessorshipsSet()) {
//                 User user = pr.getPerson().getUser();
//                 if (pr.getPermissions().getSections() && !group.isMember(user)) {
//                     group = group.grant(user);
//                 } else if (group.isMember(user)) {
//                     group = group.revoke(user);
//                 }
//             }
//             teacherRole.setGroup(group);
//         } else {
//             throw new DomainException("no.default.role");
//         }
// 
//         logger.info("Created site for execution course " + executionCourse.getSigla());
//         return newSite;
//     }
//     
// 
//     public static void updateSiteSlug(ExecutionCourse instance) {
//         instance.getSite().setSlug(ExecutionCourseSiteBuilder.formatSlugForExecutionCourse(instance));
//         instance.setSiteUrl(instance.getSite().getFullUrl());
//     }
// 
//     public static void updateProfessorship(Professorship professorship, Boolean allowAccess ) {
//         ExecutionCourse executionCourse = professorship.getExecutionCourse();
//     
//         RoleTemplate defaultTemplate = executionCourse.getSite().getDefaultRoleTemplate();
//         if (defaultTemplate != null ) {
//             Role teacherRole = executionCourse.getSite().getRolesSet().stream()
//                     .filter(role -> role.getRoleTemplate().equals(defaultTemplate))
//                     .findAny().orElseGet(() -> new Role(defaultTemplate, executionCourse.getSite()));
//         
//             Group group = teacherRole.getGroup();
//             
//             User user = professorship.getPerson().getUser();
//             if(allowAccess && !group.isMember(user)){
//                 group=group.grant(user);
//                 teacherRole.setGroup(group);
//             } else if(group.isMember(user)) {
//                 group=group.revoke(user);
//                 teacherRole.setGroup(group);
//             }
//         } else {
//             throw new DomainException("no.default.role");
//         }
//     
//     }
// }


import org.fenixedu.academic.domain.Degree;
import org.fenixedu.academic.domain.ExecutionCourse;
import org.fenixedu.cms.domain.Site;
import org.fenixedu.cms.exceptions.CmsDomainException;
import org.fenixedu.learning.domain.degree.DegreeSiteListener;
import org.fenixedu.learning.domain.executionCourse.ExecutionCourseListener;
import org.springframework.stereotype.Controller;
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.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.RedirectView;
import javax.ws.rs.core.Response;
import java.net.URI;
import java.net.URISyntaxException;
package org.fenixedu.learning.ui;



/**
 * Created by diutsu on 27/09/16.
 */
@Controller
@RequestMapping("/learning/sites/")
public class SpecificSitesAdmin {
    
    @RequestMapping(value = "/degree", method = RequestMethod.POST)
    public RedirectView getDegreeSite(@RequestParam("dsigla") String degreeSigla) throws URISyntaxException {
        Degree degree = Degree.readBySigla(degreeSigla);
        if(degree==null){
            throw CmsDomainException.notFound();
        }
        if(degree.getSite()!=null){
            Site site = degree.getSite();
            return new RedirectView(site.getEditUrl());
        }
// Path: src/main/java/org/fenixedu/learning/domain/degree/DegreeSiteListener.java
// public class DegreeSiteListener {
// 
//     @Atomic
//     public static Site create(Degree degree) {
//         LocalizedString name;
//     
//         if (degree.getPhdProgram() != null) {
//             name = new LocalizedString().with(Locale.getDefault(), degree.getPhdProgram().getPresentationName());
//         } else {
//             name = new LocalizedString().with(Locale.getDefault(), degree.getPresentationName());
//         }
//     
//         Site site = DegreeSiteBuilder.getInstance().create(name);
//     
//         RoleTemplate defaultTemplate = site.getDefaultRoleTemplate();
//         if (defaultTemplate != null ) {
//             Group group = Group.users(CoordinatorGroup.get(degree).getMembers());
//             site.getDefaultRoleTemplateRole().setGroup(group);
//         } else {
//             throw new DomainException("no.default.role");
//         }
//         
//         site.setDegree(degree);
//         site.setSlug(degree.getSigla());
//         
//         return site;
//     }
// 
// 
//     
//     
// }
// 
// Path: src/main/java/org/fenixedu/learning/domain/executionCourse/ExecutionCourseListener.java
// public class ExecutionCourseListener {
//     
// 
//     private static final Logger logger = LoggerFactory.getLogger(ExecutionCourseListener.class);
//     
//     public static Site create(ExecutionCourse executionCourse) {
//         Site newSite = ExecutionCourseSiteBuilder.getInstance().create(executionCourse);
//         
//         RoleTemplate defaultTemplate = newSite.getDefaultRoleTemplate();
//         if (defaultTemplate != null ) {
//             Role teacherRole =
//             new Role(defaultTemplate, executionCourse.getSite());
//     
//             Group group = teacherRole.getGroup();
//             for(Professorship pr : executionCourse.getProfessorshipsSet()) {
//                 User user = pr.getPerson().getUser();
//                 if (pr.getPermissions().getSections() && !group.isMember(user)) {
//                     group = group.grant(user);
//                 } else if (group.isMember(user)) {
//                     group = group.revoke(user);
//                 }
//             }
//             teacherRole.setGroup(group);
//         } else {
//             throw new DomainException("no.default.role");
//         }
// 
//         logger.info("Created site for execution course " + executionCourse.getSigla());
//         return newSite;
//     }
//     
// 
//     public static void updateSiteSlug(ExecutionCourse instance) {
//         instance.getSite().setSlug(ExecutionCourseSiteBuilder.formatSlugForExecutionCourse(instance));
//         instance.setSiteUrl(instance.getSite().getFullUrl());
//     }
// 
//     public static void updateProfessorship(Professorship professorship, Boolean allowAccess ) {
//         ExecutionCourse executionCourse = professorship.getExecutionCourse();
//     
//         RoleTemplate defaultTemplate = executionCourse.getSite().getDefaultRoleTemplate();
//         if (defaultTemplate != null ) {
//             Role teacherRole = executionCourse.getSite().getRolesSet().stream()
//                     .filter(role -> role.getRoleTemplate().equals(defaultTemplate))
//                     .findAny().orElseGet(() -> new Role(defaultTemplate, executionCourse.getSite()));
//         
//             Group group = teacherRole.getGroup();
//             
//             User user = professorship.getPerson().getUser();
//             if(allowAccess && !group.isMember(user)){
//                 group=group.grant(user);
//                 teacherRole.setGroup(group);
//             } else if(group.isMember(user)) {
//                 group=group.revoke(user);
//                 teacherRole.setGroup(group);
//             }
//         } else {
//             throw new DomainException("no.default.role");
//         }
//     
//     }
// }


// Path: src/main/java/org/fenixedu/learning/ui/SpecificSitesAdmin.java
import org.fenixedu.academic.domain.Degree;
import org.fenixedu.academic.domain.ExecutionCourse;
import org.fenixedu.cms.domain.Site;
import org.fenixedu.cms.exceptions.CmsDomainException;
import org.fenixedu.learning.domain.degree.DegreeSiteListener;
import org.fenixedu.learning.domain.executionCourse.ExecutionCourseListener;
import org.springframework.stereotype.Controller;
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.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.RedirectView;
import javax.ws.rs.core.Response;
import java.net.URI;
import java.net.URISyntaxException;

package org.fenixedu.learning.ui;



/**
 * Created by diutsu on 27/09/16.
 */
@Controller
@RequestMapping("/learning/sites/")
public class SpecificSitesAdmin {
    
    @RequestMapping(value = "/degree", method = RequestMethod.POST)
    public RedirectView getDegreeSite(@RequestParam("dsigla") String degreeSigla) throws URISyntaxException {
        Degree degree = Degree.readBySigla(degreeSigla);
        if(degree==null){
            throw CmsDomainException.notFound();
        }
        if(degree.getSite()!=null){
            Site site = degree.getSite();
            return new RedirectView(site.getEditUrl());
        }
        Site site = DegreeSiteListener.create(degree);