Skip to content

Commit

Permalink
#738 - add "read only" option on the UI + editable flag on the DB
Browse files Browse the repository at this point in the history
  • Loading branch information
cbellone committed Sep 4, 2019
1 parent d856a40 commit 6c1d935
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/java/alfio/manager/EventManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private void insertAdditionalField(EventAndOrganizationId event, AdditionalField

public void updateAdditionalField(int id, EventModification.UpdateAdditionalField f) {
String serializedRestrictedValues = toSerializedRestrictedValues(f);
ticketFieldRepository.updateRequiredAndRestrictedValues(id, f.isRequired(), serializedRestrictedValues, toSerializedDisabledValues(f), generateJsonForList(f.getLinkedCategoriesIds()));
ticketFieldRepository.updateField(id, f.isRequired(), !f.isReadOnly(), serializedRestrictedValues, toSerializedDisabledValues(f), generateJsonForList(f.getLinkedCategoriesIds()));
f.getDescription().forEach((locale, value) -> {
String val = Json.GSON.toJson(value.getDescription());
if(0 == ticketFieldRepository.updateDescription(id, locale, val)) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/alfio/model/TicketFieldConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public enum Context {
private final Integer maxLength;
private final Integer minLength;
private final boolean required;
private final boolean editable;
private final List<String> restrictedValues;
private final Context context;
private final Integer additionalServiceId;
Expand All @@ -55,6 +56,7 @@ public TicketFieldConfiguration(@Column("id") int id,
@Column("field_maxlength") Integer maxLength,
@Column("field_minlength") Integer minLength,
@Column("field_required") boolean required,
@Column("field_editable") boolean editable,
@Column("field_restricted_values") String restrictedValues,
@Column("context") Context context,
@Column("additional_service_id") Integer additionalServiceId,
Expand All @@ -68,6 +70,7 @@ public TicketFieldConfiguration(@Column("id") int id,
this.maxLength = maxLength;
this.minLength = minLength;
this.required = required;
this.editable = editable;
this.restrictedValues = restrictedValues == null ? Collections.emptyList() : Json.GSON.fromJson(restrictedValues, new TypeToken<List<String>>(){}.getType());
this.disabledValues = disabledValues == null ? Collections.emptyList() : Json.GSON.fromJson(disabledValues, new TypeToken<List<String>>(){}.getType());
this.context = context;
Expand Down Expand Up @@ -123,4 +126,8 @@ public boolean isMinLengthDefined() {
public boolean rulesApply(Integer ticketCategoryId) {
return categoryIds.isEmpty() || categoryIds.contains(ticketCategoryId);
}

public boolean isReadOnly() {
return !editable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ public class TicketFieldConfigurationDescriptionAndValue {
private final int count;
private final String value;

public String getTranslatedValue() {
if(StringUtils.isBlank(value)) {
return value;
}
if(isSelectField()) {
return ticketFieldDescription.getRestrictedValuesDescription().getOrDefault(value, "MISSING_DESCRIPTION");
}
return value;
}

public List<Triple<String, String, Boolean>> getTranslatedRestrictedValue() {
Map<String, String> description = ticketFieldDescription.getRestrictedValuesDescription();
Expand All @@ -53,11 +62,11 @@ public List<Triple<String, String, Boolean>> getTranslatedRestrictedValue() {

public List<TicketFieldValue> getFields() {
if(count == 1) {
return Collections.singletonList(new TicketFieldValue(0, 1, value));
return Collections.singletonList(new TicketFieldValue(0, 1, value, isAcceptingValues()));
}
List<String> values = StringUtils.isBlank(value) ? Collections.emptyList() : Json.fromJson(value, new TypeReference<List<String>>() {});
List<String> values = StringUtils.isBlank(value) ? Collections.emptyList() : Json.fromJson(value, new TypeReference<>() {});
return IntStream.range(0, count)
.mapToObj(i -> new TicketFieldValue(i, i+1, i < values.size() ? values.get(i) : ""))
.mapToObj(i -> new TicketFieldValue(i, i+1, i < values.size() ? values.get(i) : "", isAcceptingValues()))
.collect(Collectors.toList());

}
Expand All @@ -78,6 +87,10 @@ public String getValue() {
return value;
}

private boolean isAcceptingValues() {
return isEditable() || StringUtils.isBlank(value);
}

public boolean isBeforeStandardFields() {
return getOrder() < 0;
}
Expand All @@ -94,6 +107,7 @@ public static class TicketFieldValue {
private final int fieldIndex;
private final int fieldCounter;
private final String fieldValue;
private final Boolean editable;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ private EventModification.AdditionalField toAdditionalField(int ordinal) {
name,
code,
Boolean.TRUE.equals(required),
false,
minLength,
maxLength,
restrictedValues,
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/alfio/model/modification/EventModification.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public static class AdditionalField implements WithRestrictedValues, WithLinkedC
private final String name;
private final String type;
private final boolean required;
private final boolean readOnly;

private final Integer minLength;
private final Integer maxLength;
Expand All @@ -185,6 +186,7 @@ public AdditionalField(@JsonProperty("order") int order,
@JsonProperty("name") String name,
@JsonProperty("type") String type,
@JsonProperty("required") boolean required,
@JsonProperty("readOnly") boolean readOnly,
@JsonProperty("minLength") Integer minLength,
@JsonProperty("maxLength") Integer maxLength,
@JsonProperty("restrictedValues") List<RestrictedValue> restrictedValues,
Expand All @@ -195,6 +197,7 @@ public AdditionalField(@JsonProperty("order") int order,
this.name = name;
this.type = type;
this.required = required;
this.readOnly = readOnly;
this.minLength = minLength;
this.maxLength = maxLength;
this.restrictedValues = restrictedValues;
Expand Down Expand Up @@ -223,6 +226,7 @@ public List<Integer> getLinkedCategoriesIds() {
public static class UpdateAdditionalField implements WithRestrictedValues, WithLinkedCategories {
private final String type;
private final boolean required;
private final boolean readOnly;
private final List<String> restrictedValues;
private final Map<String, TicketFieldDescriptionModification> description;
private final List<String> disabledValues;
Expand All @@ -231,12 +235,14 @@ public static class UpdateAdditionalField implements WithRestrictedValues, WithL
@JsonCreator
public UpdateAdditionalField(@JsonProperty("type") String type,
@JsonProperty("required") boolean required,
@JsonProperty("readOnly") boolean readOnly,
@JsonProperty("restrictedValues") List<String> restrictedValues,
@JsonProperty("disabledValues") List<String> disabledValues,
@JsonProperty("description") Map<String, TicketFieldDescriptionModification> description,
@JsonProperty("categoryIds") List<Integer> linkedCategoriesIds) {
this.type = type;
this.required = required;
this.readOnly = readOnly;
this.restrictedValues = restrictedValues;
this.disabledValues = disabledValues;
this.description = description;
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/alfio/repository/FieldRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ AffectedRowCountAndKey<Integer> insertConfiguration(@Bind("eventId") int eventId
@Bind("maxLength") Integer maxLength, @Bind("minLength") Integer minLength, @Bind("required") boolean required, @Bind("context") TicketFieldConfiguration.Context context,
@Bind("additionalServiceId") Integer additionalServiceId, @Bind("categoryIds") String categoryIdsJson);

@Query("update ticket_field_configuration set field_required = :required, field_restricted_values = :restrictedValues, field_disabled_values = :disabledValues, ticket_category_ids = :categoryIds where id = :id")
int updateRequiredAndRestrictedValues(@Bind("id") int id, @Bind("required") boolean required, @Bind("restrictedValues") String restrictedValues, @Bind("disabledValues") String disabledValues, @Bind("categoryIds") String linkedCategoryIds);
@Query("update ticket_field_configuration set field_required = :required, field_editable = :editable, field_restricted_values = :restrictedValues, field_disabled_values = :disabledValues, ticket_category_ids = :categoryIds where id = :id")
int updateField(@Bind("id") int id,
@Bind("required") boolean required,
@Bind("editable") boolean editable,
@Bind("restrictedValues") String restrictedValues,
@Bind("disabledValues") String disabledValues,
@Bind("categoryIds") String linkedCategoryIds);

@Query("insert into ticket_field_description(ticket_field_configuration_id_fk, field_locale, description) values (:ticketConfigurationId, :locale, :description)")
int insertDescription(@Bind("ticketConfigurationId") int ticketConfigurationId, @Bind("locale") String locale, @Bind("description") String description);
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/alfio/repository/TicketFieldRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ public interface TicketFieldRepository extends FieldRepository {
default void updateOrInsert(Map<String, List<String>> values, int ticketId, int eventId) {
Map<String, TicketFieldValue> toUpdate = findAllByTicketIdGroupedByName(ticketId);
values = Optional.ofNullable(values).orElseGet(Collections::emptyMap);
Map<String, Integer> fieldNameToId = findAdditionalFieldsForEvent(eventId).stream().collect(Collectors.toMap(TicketFieldConfiguration::getName, TicketFieldConfiguration::getId));
var additionalFieldsForEvent = findAdditionalFieldsForEvent(eventId);
var readOnlyFields = additionalFieldsForEvent.stream().filter(TicketFieldConfiguration::isReadOnly).map(TicketFieldConfiguration::getName).collect(Collectors.toSet());
Map<String, Integer> fieldNameToId = additionalFieldsForEvent.stream().collect(Collectors.toMap(TicketFieldConfiguration::getName, TicketFieldConfiguration::getId));

values.forEach((fieldName, fieldValues) -> {
String fieldValue;
Expand All @@ -103,11 +105,13 @@ default void updateOrInsert(Map<String, List<String>> values, int ticketId, int

boolean isNotBlank = StringUtils.isNotBlank(fieldValue);
if(toUpdate.containsKey(fieldName)) {
TicketFieldValue field = toUpdate.get(fieldName);
if(isNotBlank) {
updateValue(field.getTicketId(), field.getTicketFieldConfigurationId(), fieldValue);
} else {
deleteValue(field.getTicketId(), field.getTicketFieldConfigurationId());
if(!readOnlyFields.contains(fieldName)) {
TicketFieldValue field = toUpdate.get(fieldName);
if(isNotBlank) {
updateValue(field.getTicketId(), field.getTicketFieldConfigurationId(), fieldValue);
} else {
deleteValue(field.getTicketId(), field.getTicketFieldConfigurationId());
}
}
} else if(fieldNameToId.containsKey(fieldName) && isNotBlank) {
insertValue(ticketId, fieldNameToId.get(fieldName), fieldValue);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--
-- This file is part of alf.io.
--
-- alf.io is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- alf.io is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with alf.io. If not, see <http://www.gnu.org/licenses/>.
--

alter table ticket_field_configuration add column field_editable boolean not null default true;
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ <h2><span ng-if="!addNewField">Edit</span><span ng-if="addNewField">Add new</spa
</div>
</div>
</div>
<div class="form-group" ng-if="!addNewField">
<label class="col-xs-2 control-label">Read Only</label>
<div class="col-xs-2">
<div class="checkbox">
<label><input type="checkbox" data-ng-model="field.readOnly" name="fieldReadOnly" id="fieldReadOnly"></label>
</div>
</div>
</div>
</div>
<div class="page-header" ng-if="field.type && field.type !== 'country'">
<h3>Configuration</h3>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ <h5 class="text-muted">The following data will be collected (full name, e-mail a
<div class="col-sm-4"><strong>Required</strong></div>
<div class="col-sm-8">{{field.required}}</div>
</div>
<div class="row" data-ng-if="field.readOnly">
<div class="col-sm-4"><strong>Read Only</strong></div>
<div class="col-sm-8">{{field.readOnly}}</div>
</div>
</div>
</div>
<hr data-ng-if="field.type == 'select'" />
Expand Down

0 comments on commit 6c1d935

Please sign in to comment.