Skip to content

Commit

Permalink
[SYNCOPE-1840] Reworking MacroTask's form properties storage
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgrosso committed Nov 7, 2024
1 parent db69345 commit ea65d9f
Show file tree
Hide file tree
Showing 22 changed files with 169 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ public void setObject(final String object) {
LOG.error("Invalid Locale: {}", validatable.getValue(), e);
validatable.error(new ValidationError("Invalid Locale: " + validatable.getValue()));

RequestCycle.get().find(AjaxRequestTarget.class).
ifPresent(target -> target.add(Labels.this));
RequestCycle.get().find(AjaxRequestTarget.class).ifPresent(t -> t.add(Labels.this));
}
});
item.add(locale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.syncope.client.console.SyncopeConsoleSession;
import org.apache.syncope.client.lib.batch.BatchRequest;
import org.apache.syncope.client.ui.commons.DateOps;
import org.apache.syncope.common.lib.form.SyncopeForm;
Expand Down Expand Up @@ -194,7 +195,8 @@ public <T extends TaskTO> T readTask(final TaskType type, final String taskKey)
}

public SyncopeForm getMacroTaskForm(final String taskKey) {
return getService(TaskService.class).getMacroTaskForm(taskKey);
return getService(TaskService.class).
getMacroTaskForm(taskKey, SyncopeConsoleSession.get().getLocale().toLanguageTag());
}

public void delete(final TaskType type, final String taskKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
Expand Down Expand Up @@ -89,20 +90,19 @@ public FormPropertyDefsPanel(
protected void populateItem(final ListItem<FormPropertyDefTO> item) {
FormPropertyDefTO fpd = item.getModelObject();

AjaxTextFieldPanel key = new AjaxTextFieldPanel(
"key",
"key",
new PropertyModel<>(fpd, "key"),
true);
item.add(key.setRequired(true).hideLabel());

AjaxTextFieldPanel name = new AjaxTextFieldPanel(
"name",
"name",
new PropertyModel<>(fpd, "name"),
true);
item.add(name.setRequired(true).hideLabel());

AjaxGridFieldPanel<String, Locale, String> labels = new AjaxGridFieldPanel<>(
"labels",
"labels",
new PropertyModel<>(fpd, "labels"));
item.add(labels.hideLabel());

AjaxCheckBoxPanel readable = new AjaxCheckBoxPanel(
"readable",
"readable",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
wicket:id="propertyDefContainer">
<tbody>
<tr>
<th><wicket:message key="key"/></th>
<th><wicket:message key="name"/></th>
<th><wicket:message key="labels"/></th>
<th><wicket:message key="readable"/></th>
<th><wicket:message key="writable"/></th>
<th><wicket:message key="required"/></th>
Expand All @@ -37,10 +37,10 @@

<tr wicket:id="propertyDefs">
<td>
<span wicket:id="key"/>
<span wicket:id="name"/>
</td>
<td>
<span wicket:id="name"/>
<span wicket:id="labels"/>
</td>
<td>
<span wicket:id="readable"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ enumValues=Values
datePattern=Pattern
dropdownSingleSelection=Single Selection
dropdownFreeForm=Free Form
labels=Labels
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ enumValues=Values
datePattern=Pattern
dropdownSingleSelection=Single Selection
dropdownFreeForm=Free Form
labels=Labels
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ enumValues=Valori
datePattern=Modello
dropdownSingleSelection=Selezione Singola
dropdownFreeForm=Modalit\u00e0 libera
labels=Etichette
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ enumValues=Values
datePattern=Pattern
dropdownSingleSelection=Single Selection
dropdownFreeForm=Free Form
labels=Labels
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ enumValues=Values
datePattern=Pattern
dropdownSingleSelection=Single Selection
dropdownFreeForm=Free Form
labels=Labels
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ enumValues=Values
datePattern=Pattern
dropdownSingleSelection=Single Selection
dropdownFreeForm=Free Form
labels=Labels
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
*/
package org.apache.syncope.common.lib.to;

import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.commons.lang3.builder.EqualsBuilder;
Expand All @@ -33,6 +36,8 @@ public class FormPropertyDefTO implements NamedEntityTO {

private String name;

private final Map<Locale, String> labels = new HashMap<>();

private FormPropertyType type;

private boolean readable = true;
Expand Down Expand Up @@ -71,6 +76,15 @@ public void setName(final String name) {
this.name = name;
}

@JsonIgnore
public String getLabel(final Locale locale) {
return labels.getOrDefault(locale, key);
}

public Map<Locale, String> getLabels() {
return labels;
}

public FormPropertyType getType() {
return type;
}
Expand Down Expand Up @@ -144,6 +158,7 @@ public int hashCode() {
return new HashCodeBuilder().
append(key).
append(name).
append(labels).
append(type).
append(readable).
append(writable).
Expand Down Expand Up @@ -171,6 +186,7 @@ public boolean equals(final Object obj) {
return new EqualsBuilder().
append(key, other.key).
append(name, other.name).
append(labels, other.labels).
append(type, other.type).
append(readable, other.readable).
append(writable, other.writable).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,13 @@ Response purgePropagations(
* Fetches the form to fill and submit for execution, for the given macro task (if defined).
*
* @param key macro task key
* @param locale form locale
* @return the form to fill and submit for execution, for the given macro task (if defined)
*/
@GET
@Path("MACRO/{key}/form")
@Produces({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML })
SyncopeForm getMacroTaskForm(@NotNull @PathParam("key") String key);
SyncopeForm getMacroTaskForm(@NotNull @PathParam("key") String key, @NotNull @QueryParam("locale") String locale);

/**
* Executes the macro task matching the given specs, with the provided form as input.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -284,14 +285,14 @@ public <T extends TaskTO> T read(final TaskType type, final String key, final bo

@PreAuthorize("hasRole('" + IdRepoEntitlement.TASK_READ + "')")
@Transactional(readOnly = true)
public SyncopeForm getMacroTaskForm(final String key) {
public SyncopeForm getMacroTaskForm(final String key, final Locale locale) {
MacroTask task = taskDAO.find(key).
filter(MacroTask.class::isInstance).map(MacroTask.class::cast).
orElseThrow(() -> new NotFoundException("MacroTask " + key));

securityChecks(IdRepoEntitlement.TASK_READ, task.getRealm().getFullPath());

return binder.getMacroTaskForm(task);
return binder.getMacroTaskForm(task, locale);
}

protected ExecTO doExecute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@
import java.net.URI;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Locale;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.core.Response;
import org.apache.commons.lang3.LocaleUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.syncope.common.lib.SyncopeClientException;
import org.apache.syncope.common.lib.form.SyncopeForm;
import org.apache.syncope.common.lib.to.ExecTO;
import org.apache.syncope.common.lib.to.PagedResult;
import org.apache.syncope.common.lib.to.SchedTaskTO;
import org.apache.syncope.common.lib.to.TaskTO;
import org.apache.syncope.common.lib.types.ClientExceptionType;
import org.apache.syncope.common.lib.types.ExecStatus;
import org.apache.syncope.common.lib.types.TaskType;
import org.apache.syncope.common.rest.api.RESTHeaders;
Expand Down Expand Up @@ -114,8 +118,19 @@ public Response purgePropagations(
}

@Override
public SyncopeForm getMacroTaskForm(final String key) {
return logic.getMacroTaskForm(key);
public SyncopeForm getMacroTaskForm(final String key, final String locale) {
Locale localeObj = null;
try {
localeObj = LocaleUtils.toLocale(locale);
} catch (Exception e) {
LOG.error("While attempting to convert {} to Locale", locale, e);

SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidEntity);
sce.getElements().add("Invalid locale '" + locale + "'");
throw sce;
}

return logic.getMacroTaskForm(key, localeObj);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
*/
package org.apache.syncope.core.persistence.api.entity.task;

import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;
import org.apache.syncope.common.lib.form.FormPropertyType;
import org.apache.syncope.core.persistence.api.entity.ProvidedKeyEntity;
import org.apache.syncope.core.persistence.api.entity.Entity;

public interface FormPropertyDef extends ProvidedKeyEntity {
public interface FormPropertyDef extends Entity {

MacroTask getMacroTask();

Expand All @@ -33,6 +35,10 @@ public interface FormPropertyDef extends ProvidedKeyEntity {

void setName(String name);

Optional<String> getLabel(Locale locale);

Map<Locale, String> getLabels();

FormPropertyType getType();

void setType(FormPropertyType type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.apache.syncope.core.persistence.api.entity.task.TaskUtils;
import org.apache.syncope.core.persistence.api.entity.task.TaskUtilsFactory;
import org.apache.syncope.core.persistence.jpa.entity.task.JPAMacroTask;
import org.apache.syncope.core.persistence.jpa.entity.task.JPAMacroTaskCommand;
import org.apache.syncope.core.persistence.jpa.entity.task.JPANotificationTask;
import org.apache.syncope.core.persistence.jpa.entity.task.JPAPropagationTask;
import org.apache.syncope.core.persistence.jpa.entity.task.JPAPropagationTaskExec;
Expand Down Expand Up @@ -198,8 +199,9 @@ public List<MacroTask> findByRealm(final Realm realm) {

@Override
public List<MacroTask> findByCommand(final Implementation command) {
TypedQuery<MacroTask> query = entityManager().createQuery("SELECT e FROM " + JPAMacroTask.class.getSimpleName()
+ " e WHERE :command MEMBER OF e.commands", MacroTask.class);
TypedQuery<MacroTask> query = entityManager().createQuery(
"SELECT e.macroTask FROM " + JPAMacroTaskCommand.class.getSimpleName() + " e "
+ "WHERE e.command=:command", MacroTask.class);
query.setParameter("command", command);

return query.getResultList();
Expand Down
Loading

0 comments on commit ea65d9f

Please sign in to comment.