parent
a549db6035
commit
8c6f68eb2b
|
|
@ -29,6 +29,7 @@ import org.libreccm.core.CoreConstants;
|
|||
import org.libreccm.security.AuthorizationRequired;
|
||||
import org.libreccm.security.RequiresPrivilege;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
|
|
@ -145,17 +146,37 @@ public class CategoriesApi {
|
|||
|
||||
@PUT
|
||||
@Path("/{domainIdentifier}/{path:^[\\w\\-/]+$}")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@AuthorizationRequired
|
||||
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public CategoryData updateCategory(
|
||||
public Response updateCategory(
|
||||
@PathParam("domainIdentifier") final String domainIdentifierParam,
|
||||
@PathParam("path") final String categoryPathTokens,
|
||||
@PathParam("path") final String categoryPath,
|
||||
final CategoryData categoryData
|
||||
) {
|
||||
throw new UnsupportedOperationException();
|
||||
final Domain domain = repository.findDomain(domainIdentifierParam);
|
||||
final Category category = categoryRepository
|
||||
.findByPath(domain, categoryPath)
|
||||
.orElseThrow(
|
||||
() -> new WebApplicationException(
|
||||
String.format(
|
||||
"No category with path %s in Domain %s found.",
|
||||
categoryPath,
|
||||
domain.getDomainKey()
|
||||
),
|
||||
Response.Status.NOT_FOUND
|
||||
)
|
||||
);
|
||||
|
||||
return updateCategory(category, categoryData)
|
||||
.entity(
|
||||
String.format(
|
||||
"Category %s/%s updated successfully.",
|
||||
domain.getDomainKey(),
|
||||
categoryPath
|
||||
)
|
||||
).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
|
|
@ -165,11 +186,27 @@ public class CategoriesApi {
|
|||
@AuthorizationRequired
|
||||
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public CategoryData updateCategory(
|
||||
@PathParam("catgoryId") final long categoryIdParam,
|
||||
public Response updateCategory(
|
||||
@PathParam("catgoryId") final long categoryId,
|
||||
final CategoryData categoryData
|
||||
) {
|
||||
throw new UnsupportedOperationException();
|
||||
final Category category = categoryRepository
|
||||
.findById(categoryId)
|
||||
.orElseThrow(
|
||||
() -> new WebApplicationException(
|
||||
String.format(
|
||||
"No Category with ID %d found.", categoryId
|
||||
),
|
||||
Response.Status.NOT_FOUND
|
||||
)
|
||||
);
|
||||
|
||||
return updateCategory(category, categoryData)
|
||||
.entity(
|
||||
String.format(
|
||||
"Category %d updated successfully.", categoryId
|
||||
)
|
||||
).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
|
|
@ -179,11 +216,89 @@ public class CategoriesApi {
|
|||
@AuthorizationRequired
|
||||
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public CategoryData updateCategory(
|
||||
@PathParam("catgoryUid") final String categoryIdParam,
|
||||
public Response updateCategory(
|
||||
@PathParam("catgoryUuid") final String uuid,
|
||||
final CategoryData categoryData
|
||||
) {
|
||||
throw new UnsupportedOperationException();
|
||||
final Category category = categoryRepository
|
||||
.findByUuid(uuid)
|
||||
.orElseThrow(
|
||||
() -> new WebApplicationException(
|
||||
String.format(
|
||||
"No Category with UIID %s found.", uuid
|
||||
),
|
||||
Response.Status.NOT_FOUND
|
||||
)
|
||||
);
|
||||
|
||||
return updateCategory(category, categoryData)
|
||||
.entity(
|
||||
String.format(
|
||||
"Category %s updated successfully.", uuid
|
||||
)
|
||||
).build();
|
||||
}
|
||||
|
||||
private Response.ResponseBuilder updateCategory(
|
||||
final Category category,
|
||||
final CategoryData categoryData
|
||||
) {
|
||||
if (category.getParentCategory() != null
|
||||
&& categoryData.getParentCategory() != null) {
|
||||
// Check if parent category changed. If yes move category
|
||||
if (!category.getParentCategory().getUuid().equals(
|
||||
categoryData.getParentCategory().getUuid()
|
||||
)) {
|
||||
final Category target = categoryRepository
|
||||
.findByUuid(categoryData.getParentCategory().getUuid())
|
||||
.orElseThrow(
|
||||
() -> new WebApplicationException(
|
||||
String.format(
|
||||
"Target category with UUID %s not found.",
|
||||
categoryData.getParentCategory().getUuid()
|
||||
)
|
||||
)
|
||||
);
|
||||
categoryManager.addSubCategoryToCategory(category, target);
|
||||
}
|
||||
}
|
||||
|
||||
boolean updated = false;
|
||||
|
||||
if (category.getCategoryOrder() != categoryData.getCategoryOrder()) {
|
||||
category.setCategoryOrder(categoryData.getCategoryOrder());
|
||||
updated = true;
|
||||
}
|
||||
|
||||
if (!Objects.equals(
|
||||
category.getDescription(), categoryData.getDescription()
|
||||
)) {
|
||||
category.setDescription(categoryData.getDescription());
|
||||
updated = true;
|
||||
}
|
||||
|
||||
if (!Objects.equals(category.getName(), categoryData.getName())) {
|
||||
category.setName(categoryData.getName());
|
||||
updated = true;
|
||||
}
|
||||
|
||||
if (!category.getTitle().equals(categoryData.getTitle())) {
|
||||
category.setTitle(categoryData.getTitle());
|
||||
updated = true;
|
||||
}
|
||||
|
||||
if (!Objects.equals(
|
||||
category.getUniqueId(), categoryData.getUniqueId()
|
||||
)) {
|
||||
category.setUniqueId(categoryData.getUniqueId());
|
||||
updated = true;
|
||||
}
|
||||
|
||||
if (updated) {
|
||||
categoryRepository.save(category);
|
||||
}
|
||||
|
||||
return Response.ok();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
|
|
|
|||
Loading…
Reference in New Issue