forked from sakaiproject/sakai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SAK-49258 Webapi: Add endpoints to get and bulk update various entiti…
…es (sakaiproject#12315)
- Loading branch information
Showing
8 changed files
with
1,208 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
webapi/src/main/java/org/sakaiproject/webapi/beans/LessonItemRestBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/****************************************************************************** | ||
* Copyright 2023 sakaiproject.org Licensed under the Educational | ||
* Community 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://opensource.org/licenses/ECL-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. | ||
******************************************************************************/ | ||
package org.sakaiproject.webapi.beans; | ||
|
||
import org.apache.commons.lang3.ObjectUtils; | ||
import org.sakaiproject.lessonbuildertool.SimplePageItem; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonInclude.Include; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@JsonInclude(Include.NON_NULL) | ||
public class LessonItemRestBean { | ||
|
||
|
||
private Long id; | ||
private Long pageId; | ||
private Integer sequence; | ||
private Integer type; | ||
private String contentRef; | ||
private String title; | ||
private String format; | ||
private String html; | ||
|
||
|
||
@JsonIgnore | ||
public boolean isCreatable() { | ||
boolean requiredFieldsPresent = ObjectUtils.allNotNull( | ||
pageId, | ||
type, | ||
title | ||
); | ||
|
||
return requiredFieldsPresent; | ||
} | ||
|
||
public static LessonItemRestBean of(SimplePageItem lessonItem) { | ||
return LessonItemRestBean.builder() | ||
.id(lessonItem.getId()) | ||
.pageId(lessonItem.getPageId()) | ||
.sequence(lessonItem.getSequence()) | ||
.type(lessonItem.getType()) | ||
.contentRef(lessonItem.getSakaiId()) | ||
.title(lessonItem.getName()) | ||
.format(lessonItem.getFormat()) | ||
.html(lessonItem.getHtml()) | ||
.build(); | ||
} | ||
} | ||
|
144 changes: 144 additions & 0 deletions
144
webapi/src/main/java/org/sakaiproject/webapi/beans/SiteEntityRestBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/****************************************************************************** | ||
* Copyright 2023 sakaiproject.org Licensed under the Educational | ||
* Community 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://opensource.org/licenses/ECL-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. | ||
******************************************************************************/ | ||
package org.sakaiproject.webapi.beans; | ||
|
||
import java.time.Instant; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.Date; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.sakaiproject.api.app.messageforums.OpenForum; | ||
import org.sakaiproject.assignment.api.model.Assignment; | ||
import org.sakaiproject.content.api.ContentEntity; | ||
import org.sakaiproject.entity.api.ResourceProperties; | ||
import org.sakaiproject.tool.assessment.facade.PublishedAssessmentFacade; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonInclude.Include; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@JsonInclude(Include.NON_NULL) | ||
public class SiteEntityRestBean { | ||
|
||
private static final String SITE_SEGMENT = "/site/"; | ||
private static final String GROUP_SEGMENT = "/group/"; | ||
|
||
private String id; | ||
private SiteEntityType type; | ||
private String title; | ||
private Instant openDate; | ||
private Instant dueDate; | ||
private Instant closeDate; | ||
private Boolean dateRestricted; | ||
private Set<String> groupRefs; | ||
private Set<TimeExceptionRestBean> timeExceptions; | ||
|
||
@SuppressWarnings("unchecked") | ||
public static SiteEntityRestBean of(PublishedAssessmentFacade assessment, | ||
Set<TimeExceptionRestBean> timeExceptions) { | ||
String siteId = assessment.getOwnerSiteId(); | ||
Set<String> groupRefs = Optional.ofNullable(assessment.getReleaseToGroups()) | ||
.map(Map::keySet) | ||
.map(groupIds -> (Set<String>) groupIds) | ||
.map(groupIds -> groupIds.stream() | ||
.map(groupId -> SITE_SEGMENT + siteId + GROUP_SEGMENT + groupId) | ||
.collect(Collectors.toSet())) | ||
.orElse(Collections.emptySet()); | ||
String assessmentId = Optional.ofNullable(assessment.getPublishedAssessmentId()) | ||
.map(id -> id.toString()) | ||
.orElse(null); | ||
|
||
return SiteEntityRestBean.builder() | ||
.id(assessmentId) | ||
.type(SiteEntityType.ASSESSMENT) | ||
.title(assessment.getTitle()) | ||
.openDate(Optional.ofNullable(assessment.getStartDate()).map(Date::toInstant).orElse(null)) | ||
.dueDate(Optional.ofNullable(assessment.getDueDate()).map(Date::toInstant).orElse(null)) | ||
.closeDate(Optional.ofNullable(assessment.getRetractDate()).map(Date::toInstant).orElse(null)) | ||
.dateRestricted(true) | ||
.groupRefs(groupRefs) | ||
.timeExceptions(timeExceptions) | ||
.build(); | ||
} | ||
|
||
public static SiteEntityRestBean of(Assignment assignment) { | ||
Set<String> groupRefs = Assignment.Access.GROUP.equals(assignment.getTypeOfAccess()) | ||
? Set.copyOf(assignment.getGroups()) | ||
: Collections.emptySet(); | ||
|
||
return SiteEntityRestBean.builder() | ||
.id(assignment.getId()) | ||
.type(SiteEntityType.ASSIGNMENT) | ||
.title(assignment.getTitle()) | ||
.openDate(assignment.getOpenDate()) | ||
.dueDate(assignment.getDueDate()) | ||
.closeDate(assignment.getCloseDate()) | ||
.dateRestricted(true) | ||
.groupRefs(groupRefs) | ||
.build(); | ||
} | ||
|
||
public static SiteEntityRestBean of(OpenForum forum) { | ||
return SiteEntityRestBean.builder() | ||
.id(Optional.ofNullable(forum.getId()).map(id -> id.toString()).orElse(null)) | ||
.type(SiteEntityType.FORUM) | ||
.title(forum.getTitle()) | ||
.openDate(Optional.ofNullable(forum.getOpenDate()).map(Date::toInstant).orElse(null)) | ||
.closeDate(Optional.ofNullable(forum.getCloseDate()).map(Date::toInstant).orElse(null)) | ||
.dateRestricted(forum.getAvailabilityRestricted()) | ||
.build(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static SiteEntityRestBean of(ContentEntity resource) { | ||
boolean dateRestricted = resource.getReleaseInstant() != null || resource.getRetractInstant() != null; | ||
SiteEntityType type = resource.isResource() ? SiteEntityType.RESOURCE : SiteEntityType.RESOURCE_FOLDER; | ||
|
||
return SiteEntityRestBean.builder() | ||
.id(resource.getId()) | ||
.type(type) | ||
.title(resource.getProperties().getProperty(ResourceProperties.PROP_DISPLAY_NAME)) | ||
.openDate(resource.getReleaseInstant()) | ||
.closeDate(resource.getRetractInstant()) | ||
.groupRefs(Set.copyOf(resource.getGroups())) | ||
.dateRestricted(dateRestricted) | ||
.build(); | ||
} | ||
|
||
public static Comparator<SiteEntityRestBean> comparator() { | ||
return Comparator.comparing(SiteEntityRestBean::getType) | ||
.thenComparing(SiteEntityRestBean::getTitle); | ||
} | ||
|
||
|
||
public enum SiteEntityType { | ||
ASSESSMENT, | ||
ASSIGNMENT, | ||
FORUM, | ||
RESOURCE, | ||
RESOURCE_FOLDER, | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
webapi/src/main/java/org/sakaiproject/webapi/beans/TimeExceptionRestBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/****************************************************************************** | ||
* Copyright 2023 sakaiproject.org Licensed under the Educational | ||
* Community 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://opensource.org/licenses/ECL-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. | ||
******************************************************************************/ | ||
package org.sakaiproject.webapi.beans; | ||
|
||
import java.time.Instant; | ||
import java.util.Date; | ||
import java.util.Optional; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.sakaiproject.tool.assessment.data.dao.assessment.ExtendedTime; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonInclude.Include; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@EqualsAndHashCode(of = "forEntityRef") | ||
@JsonInclude(Include.NON_NULL) | ||
public class TimeExceptionRestBean { | ||
|
||
private static final String SITE_SEGMENT = "/site/"; | ||
private static final String GROUP_SEGMENT = "/group/"; | ||
private static final String USER_SEGMENT = "/user/"; | ||
|
||
private String forEntityRef; | ||
private Instant openDate; | ||
private Instant dueDate; | ||
private Instant closeDate; | ||
|
||
|
||
@JsonIgnore | ||
public boolean isValid() { | ||
boolean refValid = StringUtils.containsAny(forEntityRef, GROUP_SEGMENT, USER_SEGMENT); | ||
|
||
boolean datesValid = openDate != null && dueDate != null | ||
&& (dueDate.isAfter(openDate) || dueDate.equals(openDate)) | ||
&& (closeDate == null || closeDate.isAfter(dueDate) || closeDate.equals(dueDate)); | ||
|
||
return refValid && datesValid; | ||
} | ||
|
||
public ExtendedTime toExtendedTime() { | ||
ExtendedTime extendedTime = new ExtendedTime(); | ||
|
||
if (openDate != null) { | ||
extendedTime.setStartDate(Date.from(openDate)); | ||
} | ||
|
||
if (dueDate != null) { | ||
extendedTime.setDueDate(Date.from(dueDate)); | ||
} | ||
|
||
if (closeDate != null) { | ||
extendedTime.setRetractDate(Date.from(closeDate)); | ||
} | ||
|
||
if (StringUtils.contains(forEntityRef, GROUP_SEGMENT)) { | ||
extendedTime.setGroup(StringUtils.substringAfter(forEntityRef, GROUP_SEGMENT)); | ||
} else { | ||
extendedTime.setUser(StringUtils.substringAfter(forEntityRef, USER_SEGMENT)); | ||
} | ||
|
||
return extendedTime; | ||
} | ||
|
||
public static TimeExceptionRestBean of(String siteId, ExtendedTime extendedTime) { | ||
String entityRef = Optional.ofNullable(StringUtils.trimToNull(extendedTime.getUser())) | ||
.map(userId -> USER_SEGMENT + userId) | ||
.or(() -> Optional.ofNullable(StringUtils.trimToNull(extendedTime.getGroup())) | ||
.map(groupId -> SITE_SEGMENT + siteId + GROUP_SEGMENT + groupId)) | ||
.orElse(null); | ||
return TimeExceptionRestBean.builder() | ||
.forEntityRef(entityRef) | ||
.openDate(Optional.ofNullable(extendedTime.getStartDate()).map(Date::toInstant).orElse(null)) | ||
.dueDate(Optional.ofNullable(extendedTime.getDueDate()).map(Date::toInstant).orElse(null)) | ||
.closeDate(Optional.ofNullable(extendedTime.getRetractDate()).map(Date::toInstant).orElse(null)) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.