-
Notifications
You must be signed in to change notification settings - Fork 342
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
fix: Add templateid column in program rule action [DHIS2-17515] #17723
base: master
Are you sure you want to change the base?
Changes from all commits
756b361
94df803
8f1f8db
79bb00b
463c6df
227104a
20b56fb
d8d31ce
1dbed50
5f90766
210f479
afb0953
0f3b7f9
d9d276e
94a894d
7836f76
bf6ed7e
96c1355
ad5acfa
8e6fb00
53e0a1a
7499ad4
ea8fc4c
21dcce9
ef53bf9
ba5dab7
697b189
587d951
3b2aa26
2e93ca0
772dd14
82efeda
7de5c28
9939784
9cc579b
59a1d85
9414be5
619cffb
84eeca2
d3e959e
870573e
4d01e35
fb92d5e
66f95b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,8 +30,10 @@ | |
import java.util.Map; | ||
import java.util.function.Consumer; | ||
import javax.annotation.Nonnull; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle; | ||
import org.hisp.dhis.feedback.ErrorReport; | ||
import org.hisp.dhis.program.notification.ProgramNotificationTemplate; | ||
import org.hisp.dhis.programrule.ProgramRuleAction; | ||
import org.hisp.dhis.programrule.ProgramRuleActionType; | ||
import org.hisp.dhis.programrule.ProgramRuleActionValidationResult; | ||
|
@@ -44,6 +46,7 @@ | |
* @author Zubair Asghar | ||
*/ | ||
@Component("programRuleActionObjectBundle") | ||
@Slf4j | ||
public class ProgramRuleActionObjectBundleHook extends AbstractObjectBundleHook<ProgramRuleAction> { | ||
@Nonnull | ||
private final Map<ProgramRuleActionType, ProgramRuleActionValidator> | ||
|
@@ -69,6 +72,17 @@ public void validate( | |
} | ||
} | ||
|
||
@Override | ||
public void preCreate(ProgramRuleAction object, ObjectBundle bundle) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we getting ProgramNotificationTemplate from the DB? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. It will be |
||
getNotificationTemplate(object, bundle); | ||
} | ||
|
||
@Override | ||
public void preUpdate( | ||
ProgramRuleAction object, ProgramRuleAction persistedObject, ObjectBundle bundle) { | ||
getNotificationTemplate(object, bundle); | ||
} | ||
|
||
private ProgramRuleActionValidationResult validateProgramRuleAction( | ||
ProgramRuleAction ruleAction, ObjectBundle bundle) { | ||
ProgramRuleActionValidationResult validationResult; | ||
|
@@ -83,4 +97,32 @@ private ProgramRuleActionValidationResult validateProgramRuleAction( | |
|
||
return validationResult; | ||
} | ||
|
||
private void getNotificationTemplate(ProgramRuleAction object, ObjectBundle bundle) { | ||
if (object.getTemplateUid() == null) { | ||
// Nothing to do when templateUid is null | ||
return; | ||
} | ||
|
||
// try to get the template from the preheated bundle. | ||
ProgramNotificationTemplate template = | ||
bundle | ||
.getPreheat() | ||
.get( | ||
bundle.getPreheatIdentifier(), | ||
ProgramNotificationTemplate.class, | ||
object.getTemplateUid()); | ||
|
||
// If not found in preheated objects, fetch from the database. | ||
if (template == null) { | ||
template = manager.get(ProgramNotificationTemplate.class, object.getTemplateUid()); | ||
} | ||
|
||
if (template == null) { | ||
log.info("No ProgramNotificationTemplate found for {}: ", object.getTemplateUid()); | ||
return; | ||
} | ||
|
||
object.setNotificationTemplate(template); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
-- Add the new column | ||
ALTER TABLE programruleaction ADD COLUMN IF NOT EXISTS notificationtemplateid INTEGER NULL; | ||
|
||
-- Update the new column with values from the old column | ||
UPDATE programruleaction pra | ||
SET notificationtemplateid = t.programnotificationtemplateid | ||
FROM programnotificationtemplate t | ||
WHERE pra.templateuid = t.uid; | ||
|
||
-- Remove templateUid | ||
|
||
ALTER TABLE programruleaction DROP COLUMN IF EXISTS templateuid; | ||
|
||
-- Conditionally add the foreign key constraint if it doesn't exist | ||
DO $$ | ||
BEGIN | ||
IF NOT EXISTS ( | ||
SELECT 1 | ||
FROM pg_constraint | ||
WHERE conname = 'fk_notificationtemplate' | ||
) THEN | ||
ALTER TABLE programruleaction | ||
ADD CONSTRAINT fk_notificationtemplate | ||
FOREIGN KEY (notificationtemplateid) REFERENCES programnotificationtemplate(programnotificationtemplateid); | ||
END IF; | ||
END $$; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if both templateUid and programNotificationTemplate are defined and they have different values?
Who is the winner?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
programNotificationTemplate
will be ignored if it's there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we are not changing API.