Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Db contrib/waltz 5025 historical question responses #5052

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ public List<SurveyInstanceQuestionResponse> findForInstance(long surveyInstanceI
return ImmutableSurveyInstanceQuestionResponse
.copyOf(r)
.withQuestionResponse(ImmutableSurveyQuestionResponse
.copyOf(r.questionResponse())
.withListResponse(listResponse));
.copyOf(r.questionResponse())
.withListResponse(listResponse));
} else {
return r;
}
Expand Down Expand Up @@ -246,6 +246,57 @@ public int deleteForSurveyRun(long surveyRunId) {
}


public List<SurveyInstanceQuestionResponse> findHistoricalResponsesForQuestion(Long surveyInstanceId, Long questionId){

SelectConditionStep<Record1<Long>> historicalInstanceIds = dsl
.select(SURVEY_INSTANCE.ID)
.from(SURVEY_INSTANCE)
.where(SURVEY_INSTANCE.ORIGINAL_INSTANCE_ID.eq(surveyInstanceId));

List<SurveyInstanceQuestionResponse> questionResponses = dsl
.select(entityNameField)
.select(SURVEY_QUESTION_RESPONSE.fields())
.from(SURVEY_INSTANCE)
.innerJoin(SURVEY_QUESTION_RESPONSE).on(SURVEY_QUESTION_RESPONSE.SURVEY_INSTANCE_ID.eq(SURVEY_INSTANCE.ID))
.where(SURVEY_INSTANCE.ID.in(historicalInstanceIds))
.and(SURVEY_QUESTION_RESPONSE.QUESTION_ID.eq(questionId))
.fetch(TO_DOMAIN_MAPPER);

Map<Long, List<SurveyQuestionListResponseRecord>> instanceIdToListResponses =
dsl.selectFrom(SURVEY_QUESTION_LIST_RESPONSE)
.where(SURVEY_QUESTION_LIST_RESPONSE.SURVEY_INSTANCE_ID.in(historicalInstanceIds)
.and(SURVEY_QUESTION_LIST_RESPONSE.QUESTION_ID.eq(questionId)))
.fetch()
.stream()
.collect(groupingBy(SurveyQuestionListResponseRecord::getSurveyInstanceId, toList()));

return getSurveyInstanceQuestionResponsesWithListResponse(questionResponses, instanceIdToListResponses);
}


private List<SurveyInstanceQuestionResponse> getSurveyInstanceQuestionResponsesWithListResponse(List<SurveyInstanceQuestionResponse> questionResponses, Map<Long, List<SurveyQuestionListResponseRecord>> instanceIdToListResponses) {
return questionResponses.stream()
.map(r -> {
if (instanceIdToListResponses.containsKey(r.surveyInstanceId())) {
List<String> listResponse = instanceIdToListResponses.get(r.surveyInstanceId())
.stream()
.sorted(comparingInt(lr -> lr.getPosition()))
.map(lr -> lr.getResponse())
.collect(toList());

return ImmutableSurveyInstanceQuestionResponse
.copyOf(r)
.withQuestionResponse(ImmutableSurveyQuestionResponse
.copyOf(r.questionResponse())
.withListResponse(listResponse));
} else {
return r;
}
})
.collect(toList());
}


private SurveyQuestionResponseRecord mkRecord(SurveyInstanceQuestionResponse response) {
SurveyQuestionResponse questionResponse = response.questionResponse();
Optional<EntityReference> entityResponse = questionResponse.entityResponse();
Expand Down
9 changes: 7 additions & 2 deletions waltz-ng/client/playpen/3/playpen3.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@

<div>
<waltz-section>
<waltz-change-summaries-panel parent-entity-ref="$ctrl.parentEntityRef">
</waltz-change-summaries-panel>
<waltz-historical-responses-panel ng-if="$ctrl.selectedQuestion"
parent-entity-ref="$ctrl.parentEntityRef"
question="$ctrl.selectedQuestion"
on-select="$ctrl.onSelectHistoricalResponse"
on-cancel="$ctrl.clearSelected">
</waltz-historical-responses-panel>

</waltz-section>
</div>
27 changes: 18 additions & 9 deletions waltz-ng/client/playpen/3/playpen3.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,37 @@

import {initialiseData} from "../../common/index";
import template from "./playpen3.html";
import {CORE_API} from "../../common/services/core-api-utils";
import _ from "lodash";


const initialState = {
parentEntityRef: {
id: 95,
kind: "ORG_UNIT"
id: 45832,
kind: "SURVEY_INSTANCE"
},
schemeId: 2,
selectedDate: null,
};

function controller($stateParams, serviceBroker) {
const vm = initialiseData(this, initialState);

vm.selectedDate = new Date('2020-07-07');
serviceBroker
.loadViewData(CORE_API.SurveyQuestionStore.findForInstance, [45832])
.then(r => {
console.log(r.data);
return vm.selectedQuestion = _.find(r.data, d => d.question.id === 1164)
});

// serviceBroker.loadViewData(CORE_API.ChangeLogSummariesStore.findSummariesForKindBySelector,
// ['APPLICATION', mkSelectionOptions(vm.parentEntityRef, 'EXACT')])
// .then(r => vm.data = r.data)
// .then(console.log(vm.data));
vm.onSelectHistoricalResponse = () => {
console.log("Selecting")
};

vm.clearSelected = () => {
console.log("Clear selected response")
}
}


controller.$inject = [
"$stateParams",
"ServiceBroker"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Waltz - Enterprise Architecture
* Copyright (C) 2016, 2017, 2018, 2019 Waltz open source project
* See README.md for more information
*
* Licensed under the Apache 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://www.apache.org/licenses/LICENSE-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
*
*/
import {initialiseData} from "../../../common/index";


import template from "./historical-responses-for-question-panel.html";
import {CORE_API} from "../../../common/services/core-api-utils";
import * as _ from "lodash";


const bindings = {
parentEntityRef: "<",
question: "<",
onSelect: "<",
onCancel: "<"
};


const initialState = {
historicalResponses: [],
selectedResponse: null
};


function controller($q, serviceBroker) {

const vm = initialiseData(this, initialState);

vm.$onInit = () => {
serviceBroker
.loadViewData(CORE_API.SurveyInstanceStore.findHistoricalResponses,
[vm.parentEntityRef.id, vm.question.question.id])
.then(r => vm.historicalResponses = _.chain(r.data)
.map(d => Object.assign({}, d, {responseString: vm.getResponse(d)}))
.orderBy('lastUpdatedAt', 'desc')
.uniqBy(d => d.responseString)
.value());
};

vm.onSelectResponse = (response) => {
vm.selectedResponse = response;
vm.onSelect(response);
};

vm.isSelected = (response) => {
return vm.selectedResponse === response;
};

vm.clearSelection = () => {
vm.selectedResponse = null;
vm.onCancel();
};

vm.getResponse = (response) => {

const questionResponse = response.questionResponse;

switch (vm.question.question.fieldType){
case 'BOOLEAN':
return _.get(questionResponse, 'booleanResponse', null);
case 'NUMBER':
return _.get(questionResponse, 'numberResponse', null);
case 'TEXT':
case 'TEXTAREA':
case 'DROPDOWN':
return _.get(questionResponse, 'stringResponse', null);
case 'DATE':
return _.get(questionResponse, 'dateResponse', null);
case 'DROPDOWN_MULTI_SELECT':
return _.join(_.get(questionResponse, 'listResponse', null), ', ');
case 'APPLICATION':
case 'PERSON':
return _.get(questionResponse, 'entityResponse.name', null);
default:
return null;
}
}
}


controller.$inject = [
"$q",
"ServiceBroker"
];

const component = {
bindings,
template,
controller
};

export default {
component,
id: "waltzHistoricalResponsesPanel"
};



Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<table class="table table-condensed table-hover">
<thead>
<th>Previous responses</th>
<th></th>
</thead>
<tbody ng-repeat="response in $ctrl.historicalResponses">
<tr ng-style="{'background-color': ($ctrl.isSelected(response) ? '#dbf7d2' : null) }">
<td class="clickable"
ng-click="$ctrl.onSelectResponse(response)">
<span ng-bind="response.responseString"></span>
<span class="text-muted small" ng-bind="response.lastUpdatedAt | date:'yyyy-MM-dd'"></span>
</td>
<td>
<waltz-icon name="close"
ng-if="$ctrl.isSelected(response)"
ng-click="$ctrl.clearSelection()"
class="pull-right clickable">
</waltz-icon>
</td>
</tr>
</tbody>
</table>
8 changes: 6 additions & 2 deletions waltz-ng/client/survey/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ import SurveyRunCreateGeneral from "./components/survey-run-create-general";
import SurveyRunCreateRecipient from "./components/survey-run-create-recipient";
import SurveyRunOverview from "./components/survey-run-overview";
import SurveyTemplateOverview from "./components/survey-template-overview";
import SurveyTemplateQuestionOverviewTable from "./components/question-overview-table/survey-template-question-overview-table";
import SurveyTemplateQuestionOverviewTable
from "./components/question-overview-table/survey-template-question-overview-table";
import SurveyQuestionStore from "./services/survey-question-store";
import HistoricalResponsesForQuestionPanel
from "./components/historical-responses-for-question-panel/historical-responses-for-question-panel"

export default () => {
const module = angular.module("waltz.survey", []);
Expand Down Expand Up @@ -64,7 +67,8 @@ export default () => {
registerComponents(module, [
SurveyInstanceList,
SurveyInstanceSummary,
SurveyTemplateQuestionOverviewTable
SurveyTemplateQuestionOverviewTable,
HistoricalResponsesForQuestionPanel
]);

return module.name;
Expand Down
12 changes: 12 additions & 0 deletions waltz-ng/client/survey/services/survey-instance-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ function store($http, baseApiUrl) {
.then(result => result.data);
};

const findHistoricalResponses = (id, questionId) => {
return $http
.get(`${base}/${id}/question-id/${questionId}/historical-responses`)
.then(result => result.data);
};

const saveResponse = (id, questionResponse) => {
return $http
.put(`${base}/${id}/response`, questionResponse)
Expand Down Expand Up @@ -122,6 +128,7 @@ function store($http, baseApiUrl) {
findPreviousVersions,
findRecipients,
findResponses,
findHistoricalResponses,
saveResponse,
updateStatus,
updateDueDate,
Expand Down Expand Up @@ -178,6 +185,11 @@ export const SurveyInstanceStore_API = {
serviceFnName: 'findResponses',
description: 'finds responses for a given survey instance id'
},
findHistoricalResponses: {
serviceName,
serviceFnName: 'findHistoricalResponses',
description: 'finds historical responses for a given survey instance id'
},
findPreviousVersions: {
serviceName,
serviceFnName: 'findPreviousVersions',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ public List<SurveyInstanceQuestionResponse> findResponses(long instanceId) {
}


public List<SurveyInstanceQuestionResponse> findHistoricalResponsesForQuestion(long instanceId, long questionId) {
return surveyQuestionResponseDao.findHistoricalResponsesForQuestion(instanceId, questionId);
}


public List<SurveyInstanceRecipient> findRecipients(long instanceId) {
return surveyInstanceRecipientDao.findForSurveyInstance(instanceId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void register() {
String findPreviousVersionsPath = mkPath(BASE_URL, "id", ":id", "previous-versions");
String findRecipientsPath = mkPath(BASE_URL, ":id", "recipients");
String findResponsesPath = mkPath(BASE_URL, ":id", "responses");
String findHistoricalResponsesPath = mkPath(BASE_URL, ":id", "question-id", ":questionId", "historical-responses");
String saveResponsePath = mkPath(BASE_URL, ":id", "response");
String updateStatusPath = mkPath(BASE_URL, ":id", "status");
String updateDueDatePath = mkPath(BASE_URL, ":id", "due-date");
Expand All @@ -87,6 +88,11 @@ public void register() {
ListRoute<SurveyInstanceQuestionResponse> findResponsesRoute =
(req, res) -> surveyInstanceService.findResponses(getId(req));

ListRoute<SurveyInstanceQuestionResponse> findHistoricalResponsesRoute = (req, res) -> {
long questionId = getLong(req, "questionId");
return surveyInstanceService.findHistoricalResponsesForQuestion(getId(req), questionId);
};

ListRoute<SurveyInstanceRecipient> findRecipientsRoute =
(req, res) -> surveyInstanceService.findRecipients(getId(req));

Expand Down Expand Up @@ -171,6 +177,7 @@ public void register() {
getForList(findPreviousVersionsPath, findPreviousVersionsRoute);
getForList(findRecipientsPath, findRecipientsRoute);
getForList(findResponsesPath, findResponsesRoute);
getForList(findHistoricalResponsesPath, findHistoricalResponsesRoute);
putForDatum(saveResponsePath, saveResponseRoute);
putForDatum(updateStatusPath, updateStatusRoute);
putForDatum(updateDueDatePath, updateDueDateRoute);
Expand Down