Skip to content

Commit

Permalink
Measurable rating isPrimary editor
Browse files Browse the repository at this point in the history
- all measurable rating edit operations now have an endpoint each

finos#6635
  • Loading branch information
davidwatkins73 committed Jun 9, 2023
1 parent dc5719d commit c2f4cfb
Show file tree
Hide file tree
Showing 14 changed files with 9,346 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public class MeasurableIdSelectorFactory implements IdSelectorFactory {
private final OrganisationalUnitIdSelectorFactory orgUnitIdSelectorFactory = new OrganisationalUnitIdSelectorFactory();


/**
* @param measurableId the identifier of the measurable to start from
* @return a selector which gives all measurable ids that belong to the same category as the given measurable id
*/
public static SelectConditionStep<Record1<Long>> allMeasurablesIdsInSameCategory(Long measurableId) {
return DSL
.select(MEASURABLE.ID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import static org.finos.waltz.common.SetUtilities.union;
import static org.finos.waltz.common.StringUtilities.firstChar;
import static org.finos.waltz.common.StringUtilities.notEmpty;
import static org.finos.waltz.data.measurable.MeasurableIdSelectorFactory.allMeasurablesIdsInSameCategory;
import static org.finos.waltz.schema.Tables.*;
import static org.finos.waltz.schema.tables.Application.APPLICATION;
import static org.finos.waltz.schema.tables.Measurable.MEASURABLE;
Expand Down Expand Up @@ -702,33 +701,45 @@ private SelectHavingStep<Record4<Long, Long, String, Integer>> selectAllocsToBeU
ALLOCATION.ENTITY_KIND,
DSL.cast(DSL.sum(ALLOCATION.ALLOCATION_PERCENTAGE), Integer.class).as("allocation_percentage"))
.from(ALLOCATION)
.innerJoin(valuesToBeSummed).on(ALLOCATION.ALLOCATION_SCHEME_ID.eq(valuesToBeSummed.field(ALLOCATION.ALLOCATION_SCHEME_ID))
.innerJoin(valuesToBeSummed)
.on(ALLOCATION.ALLOCATION_SCHEME_ID.eq(valuesToBeSummed.field(ALLOCATION.ALLOCATION_SCHEME_ID))
.and(ALLOCATION.ENTITY_KIND.eq(valuesToBeSummed.field(ALLOCATION.ENTITY_KIND))
.and(ALLOCATION.ENTITY_ID.eq(valuesToBeSummed.field(ALLOCATION.ENTITY_ID)))))
.where(ALLOCATION.MEASURABLE_ID.in(targetId, measurableId))
.groupBy(ALLOCATION.ALLOCATION_SCHEME_ID, ALLOCATION.ENTITY_ID, ALLOCATION.ENTITY_KIND);
}

/**
* Updates the given measurable rating to be set as primary.
* All other ratings for the same entity/category will be set to non-primary.
*
* @param tx the dsl connection to use
* @param entityId the entity id
* @param measurableId the measurbable id
*/
private void updateToPrimary(DSLContext tx, Long entityId, Long measurableId) {
tx.update(MEASURABLE_RATING)
.set(MEASURABLE_RATING.IS_PRIMARY, false)
.where(MEASURABLE_RATING.ENTITY_ID.eq(entityId))
.and(MEASURABLE_RATING.MEASURABLE_ID.eq(allMeasurablesIdsInSameCategory(measurableId)))
.execute();

tx.update(MEASURABLE_RATING)
.set(MEASURABLE_RATING.IS_PRIMARY, true)
.where(MEASURABLE_RATING.ENTITY_ID.eq(entityId))
.and(MEASURABLE_RATING.MEASURABLE_ID.eq(measurableId))
.execute();

public boolean saveRatingItem(EntityReference entityRef,
long measurableId,
String ratingCode,
String username) {
return MeasurableRatingHelper.saveRatingItem(
dsl,
entityRef,
measurableId,
ratingCode,
username);
}


public boolean saveRatingIsPrimary(EntityReference entityRef, long measurableId, boolean isPrimary, String username) {
return dsl.transactionResult(ctx -> MeasurableRatingHelper.saveRatingIsPrimary(
ctx.dsl(),
entityRef,
measurableId,
isPrimary,
username));
}


public boolean saveRatingDescription(EntityReference entityRef, long measurableId, String description, String username) {
return dsl.transactionResult(ctx -> MeasurableRatingHelper.saveRatingDescription(
ctx.dsl(),
entityRef,
measurableId,
description,
username));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package org.finos.waltz.data.measurable_rating;

import org.finos.waltz.model.EntityReference;
import org.finos.waltz.schema.tables.records.MeasurableRatingRecord;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.impl.DSL;

import static org.finos.waltz.common.DateTimeUtilities.nowUtcTimestamp;
import static org.finos.waltz.data.measurable.MeasurableIdSelectorFactory.allMeasurablesIdsInSameCategory;
import static org.finos.waltz.schema.tables.MeasurableRating.MEASURABLE_RATING;

public class MeasurableRatingHelper {

/**
* Updates the given measurable rating to be set as primary.
* All other ratings for the same entity/category will be set to non-primary.
*
* @param tx the dsl connection to use
* @param ref the entity ref
* @param measurableId the measurable id
* @param isPrimary the new value of the isPrimary flag
*/
public static boolean saveRatingIsPrimary(DSLContext tx,
EntityReference ref,
long measurableId,
boolean isPrimary,
String username) {
tx.update(MEASURABLE_RATING)
.set(MEASURABLE_RATING.IS_PRIMARY, false)
.where(MEASURABLE_RATING.ENTITY_ID.eq(ref.id())
.and(MEASURABLE_RATING.ENTITY_KIND.eq(ref.kind().name()))
.and(MEASURABLE_RATING.MEASURABLE_ID.in(allMeasurablesIdsInSameCategory(measurableId))))
.execute();

if (isPrimary) {
// only update if we are setting to true as false case dealt with above
tx.update(MEASURABLE_RATING)
.set(MEASURABLE_RATING.IS_PRIMARY, true)
.set(MEASURABLE_RATING.LAST_UPDATED_BY, username)
.set(MEASURABLE_RATING.LAST_UPDATED_AT, nowUtcTimestamp())
.where(mkPkCondition(ref, measurableId))
.execute();
}

return true;
}


public static boolean saveRatingDescription(DSLContext tx,
EntityReference entityRef,
long measurableId,
String description,
String username) {
return tx
.update(MEASURABLE_RATING)
.set(MEASURABLE_RATING.DESCRIPTION, description)
.set(MEASURABLE_RATING.LAST_UPDATED_BY, username)
.set(MEASURABLE_RATING.LAST_UPDATED_AT, nowUtcTimestamp())
.where(mkPkCondition(entityRef, measurableId))
.execute() == 1;
}


/**
* @param tx
* @param entityRef
* @param measurableId
* @param ratingCode
* @param username
* @return
*/
public static boolean saveRatingItem(DSLContext tx,
EntityReference entityRef,
long measurableId,
String ratingCode,
String username) {

boolean exists = doesRatingExist(tx, entityRef, measurableId);

if (exists) {
int rc = tx
.update(MEASURABLE_RATING)
.set(MEASURABLE_RATING.RATING, ratingCode)
.set(MEASURABLE_RATING.LAST_UPDATED_BY, username)
.set(MEASURABLE_RATING.LAST_UPDATED_AT, nowUtcTimestamp())
.where(mkPkCondition(entityRef, measurableId))
.execute();
return rc == 1;
} else {
MeasurableRatingRecord r = tx.newRecord(MEASURABLE_RATING);

r.setRating(ratingCode);
r.setDescription(null);
r.setLastUpdatedBy(username);
r.setLastUpdatedAt(nowUtcTimestamp());
r.setEntityId(entityRef.id());
r.setEntityKind(entityRef.kind().name());
r.setMeasurableId(measurableId);
r.setProvenance("waltz");

int rc = r.insert();
return rc == 1;
}
}


public static boolean doesRatingExist(DSLContext tx,
EntityReference entityRef,
long measurableId) {
return tx
.fetchExists(DSL
.select(DSL.val(1))
.from(MEASURABLE_RATING)
.where(mkPkCondition(entityRef, measurableId)));

}


private static Condition mkPkCondition(EntityReference entityRef,
long measurableId) {
return MEASURABLE_RATING.MEASURABLE_ID.eq(measurableId)
.and(MEASURABLE_RATING.ENTITY_ID.eq(entityRef.id()))
.and(MEASURABLE_RATING.ENTITY_KIND.eq(entityRef.kind().name()));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,17 @@ public RatingSchemeItem getRatingSchemeItemById(long id){
}


public List<RatingSchemeItem> findRatingSchemeItemsForEntityAndCategory(EntityReference ref, long measurableCategoryId) {
/**
* Gives a list of permissible rating items for the given entity and category.
* This method takes into account any constraining assessment associated to the category.
* If the rating is constrained the <code>isRestricted</code> flag on the returned RatingSchemeItem will be set.
*
* @param ref the entity being checked
* @param measurableCategoryId the category being checked
* @return list of permissible rating items for the given entity and category
*/
public List<RatingSchemeItem> findRatingSchemeItemsForEntityAndCategory(EntityReference ref,
long measurableCategoryId) {

Condition assessmentDefinitionJoinCondition = ASSESSMENT_DEFINITION.ID.eq(ASSESSMENT_RATING.ASSESSMENT_DEFINITION_ID)
.and(ASSESSMENT_RATING.ENTITY_ID.eq(ref.id())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.finos.waltz.model;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.finos.waltz.model.command.Command;
import org.immutables.value.Value;

@Value.Immutable
@JsonDeserialize(as= ImmutableUpdateRatingCodeCommand.class)
public abstract class UpdateRatingCodeCommand implements Command {

public abstract String newCode();

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.immutables.value.Value;
import org.finos.waltz.model.command.Command;
import org.immutables.value.Value;

@Value.Immutable
@JsonSerialize(as = ImmutableUpdateRatingCommand.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.finos.waltz.model.DescriptionProvider;
import org.finos.waltz.model.EntityReference;
import org.finos.waltz.model.LastUpdatedProvider;
import org.finos.waltz.model.Nullable;
import org.finos.waltz.model.ProvenanceProvider;
import org.immutables.value.Value;

Expand All @@ -43,6 +42,8 @@ public abstract class MeasurableRating implements
@Value.Default
public boolean isReadOnly() { return false; }

@Nullable
public abstract Boolean isPrimary();
@Value.Default
public boolean isPrimary() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@
</div>
</div>

<div class="row">
<div class="row"
ng-if="$ctrl.selected.rating">
<div class="col-sm-3">
Primary:
</div>
Expand All @@ -254,10 +255,6 @@
label-off="Not Primary"
on-toggle="$ctrl.onPrimaryToggle()">
</waltz-toggle>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<p class="help-block small">
Only one primary rating is allowed per category, selecting this will deselect any other primary ratings for this entity
</p>
Expand All @@ -266,6 +263,7 @@

<!-- COMMENT -->
<div class="row"
ng-if="$ctrl.selected.rating"
style="padding-top: 8px;">
<div class="col-sm-3">
Comments:
Expand Down
Loading

0 comments on commit c2f4cfb

Please sign in to comment.