|
| 1 | +/* |
| 2 | + * Copyright (C) 2010-2026 Evolveum and contributors |
| 3 | + * |
| 4 | + * Licensed under the EUPL-1.2 or later. |
| 5 | + */ |
| 6 | + |
| 7 | +package com.evolveum.midpoint.gui.impl.page.admin.resource.component.wizard.schemaHandling.objectType.smart.table; |
| 8 | + |
| 9 | +import java.io.Serial; |
| 10 | + |
| 11 | +import javax.xml.namespace.QName; |
| 12 | + |
| 13 | +import com.evolveum.midpoint.gui.impl.page.admin.task.component.SmartTaskProgressPanel; |
| 14 | + |
| 15 | +import org.apache.wicket.ajax.AjaxRequestTarget; |
| 16 | +import org.apache.wicket.model.IModel; |
| 17 | +import org.apache.wicket.model.Model; |
| 18 | +import org.jetbrains.annotations.NotNull; |
| 19 | +import org.jetbrains.annotations.Nullable; |
| 20 | + |
| 21 | +import com.evolveum.midpoint.gui.api.component.BasePanel; |
| 22 | +import com.evolveum.midpoint.gui.api.page.PageBase; |
| 23 | +import com.evolveum.midpoint.gui.api.util.WebModelServiceUtils; |
| 24 | +import com.evolveum.midpoint.gui.impl.page.admin.resource.component.wizard.schemaHandling.objectType.smart.component.SmartStatisticsPanel; |
| 25 | +import com.evolveum.midpoint.prism.PrismObject; |
| 26 | +import com.evolveum.midpoint.schema.util.ShadowObjectClassStatisticsTypeUtil; |
| 27 | +import com.evolveum.midpoint.smart.api.SmartIntegrationService; |
| 28 | +import com.evolveum.midpoint.task.api.Task; |
| 29 | +import com.evolveum.midpoint.util.exception.CommonException; |
| 30 | +import com.evolveum.midpoint.util.exception.SchemaException; |
| 31 | +import com.evolveum.midpoint.web.component.AjaxIconButton; |
| 32 | +import com.evolveum.midpoint.xml.ns._public.common.common_3.GenericObjectType; |
| 33 | +import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowObjectClassStatisticsType; |
| 34 | +import com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType; |
| 35 | + |
| 36 | +/** |
| 37 | + * UI button panel for displaying or regenerating object class statistics. |
| 38 | + * |
| 39 | + * <p>When clicked, the button tries to load the latest statistics for the given |
| 40 | + * resource object class. If statistics exist, they are displayed in a popup. |
| 41 | + * If they do not exist (or regeneration is forced), a background task is started |
| 42 | + * to compute new statistics and a progress popup is shown.</p> |
| 43 | + * |
| 44 | + * <p>After the computation finishes, the newly generated statistics are |
| 45 | + * automatically displayed.</p> |
| 46 | + */ |
| 47 | +public class ObjectClassStatisticsButton extends BasePanel<QName> { |
| 48 | + |
| 49 | + @Serial private static final long serialVersionUID = 1L; |
| 50 | + |
| 51 | + private static final String ID_PROCESS_STATISTICS_BUTTON = "processStatisticsButton"; |
| 52 | + |
| 53 | + private static final String OPERATION_GET_STATISTICS = "getObjectClassStatistics"; |
| 54 | + private static final String OPERATION_REGENERATE_STATISTICS = "regenerateObjectClassStatistics"; |
| 55 | + private static final String OP_LOAD_TASK = "loadTask"; |
| 56 | + |
| 57 | + private final String resourceOid; |
| 58 | + |
| 59 | + public ObjectClassStatisticsButton(String id, IModel<QName> objectClassName, String resourceOid) { |
| 60 | + super(id, objectClassName); |
| 61 | + this.resourceOid = resourceOid; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected void onInitialize() { |
| 66 | + super.onInitialize(); |
| 67 | + initLayout(); |
| 68 | + } |
| 69 | + |
| 70 | + private void initLayout() { |
| 71 | + AjaxIconButton processStatisticsButton = new AjaxIconButton( |
| 72 | + ID_PROCESS_STATISTICS_BUTTON, |
| 73 | + Model.of("fa-solid fa-chart-bar"), |
| 74 | + getMainButtonLabel()) { |
| 75 | + |
| 76 | + @Serial private static final long serialVersionUID = 1L; |
| 77 | + |
| 78 | + @Override |
| 79 | + public void onClick(AjaxRequestTarget target) { |
| 80 | + handleClick(target); |
| 81 | + } |
| 82 | + }; |
| 83 | + processStatisticsButton.showTitleAsLabel(true); |
| 84 | + add(processStatisticsButton); |
| 85 | + } |
| 86 | + |
| 87 | + protected IModel<String> getMainButtonLabel() { |
| 88 | + return createStringResource("ObjectClassStatisticsButton.processStatistics"); |
| 89 | + } |
| 90 | + |
| 91 | + private void handleClick(@NotNull AjaxRequestTarget target) { |
| 92 | + PageBase page = getPageBase(); |
| 93 | + QName objectClassName = getModelObject(); |
| 94 | + SmartIntegrationService smartIntegrationService = page.getSmartIntegrationService(); |
| 95 | + |
| 96 | + Task task = page.createSimpleTask(OPERATION_GET_STATISTICS); |
| 97 | + |
| 98 | + try { |
| 99 | + if (!forceRegeneration()) { |
| 100 | + GenericObjectType latestStatistics = |
| 101 | + smartIntegrationService.getLatestStatistics(resourceOid, objectClassName, task.getResult()); |
| 102 | + |
| 103 | + if (latestStatistics != null) { |
| 104 | + showStatisticsPopup(target, page, latestStatistics, objectClassName); |
| 105 | + return; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // No stats exist -> trigger regeneration and show progress popup |
| 110 | + String taskOid = smartIntegrationService.regenerateObjectClassStatistics( |
| 111 | + resourceOid, objectClassName, task, task.getResult()); |
| 112 | + |
| 113 | + showProgressPopup(target, getPageBase(), smartIntegrationService, objectClassName, taskOid); |
| 114 | + |
| 115 | + } catch (CommonException e) { |
| 116 | + page.error("Couldn't get object class statistics for " + objectClassName + ": " + e.getMessage()); |
| 117 | + target.add(page.getFeedbackPanel()); |
| 118 | + } finally { |
| 119 | + task.getResult().computeStatusIfUnknown(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + protected boolean forceRegeneration() { |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + private void showStatisticsPopup( |
| 128 | + @NotNull AjaxRequestTarget target, |
| 129 | + @NotNull PageBase pageBase, @NotNull GenericObjectType statisticsObject, |
| 130 | + @NotNull QName objectClassName) throws SchemaException { |
| 131 | + |
| 132 | + ShadowObjectClassStatisticsType statistics = |
| 133 | + ShadowObjectClassStatisticsTypeUtil.getStatisticsRequired(statisticsObject); |
| 134 | + |
| 135 | + SmartStatisticsPanel statisticsPanel = new SmartStatisticsPanel( |
| 136 | + pageBase.getMainPopupBodyId(), |
| 137 | + () -> statistics, |
| 138 | + resourceOid, |
| 139 | + objectClassName); |
| 140 | + |
| 141 | + pageBase.replaceMainPopup(statisticsPanel, target); |
| 142 | + } |
| 143 | + |
| 144 | + private void showProgressPopup( |
| 145 | + @NotNull AjaxRequestTarget target, |
| 146 | + @NotNull PageBase pageBase, |
| 147 | + @NotNull SmartIntegrationService smartIntegrationService, |
| 148 | + @NotNull QName objectClassName, |
| 149 | + @NotNull String taskOid) { |
| 150 | + |
| 151 | + SmartTaskProgressPanel panel = new SmartTaskProgressPanel( |
| 152 | + pageBase.getMainPopupBodyId(), |
| 153 | + createStringResource("ObjectClassStatisticsButton.regeneratingStatistics"), |
| 154 | + createStringResource("ObjectClassStatisticsButton.regeneratingStatistics.subText", objectClassName.getLocalPart()), |
| 155 | + () -> loadTask(pageBase, taskOid)) { |
| 156 | + |
| 157 | + @Override |
| 158 | + protected void onShowResults(AjaxRequestTarget target) { |
| 159 | + onProgressFinished(target, pageBase, smartIntegrationService, objectClassName); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + protected IModel<String> getStopButtonLabel() { |
| 164 | + return createStringResource("ObjectClassStatisticsButton.stopRegeneration"); |
| 165 | + } |
| 166 | + }; |
| 167 | + |
| 168 | + pageBase.replaceMainPopup(panel, target); |
| 169 | + } |
| 170 | + |
| 171 | + private void onProgressFinished( |
| 172 | + @NotNull AjaxRequestTarget target, |
| 173 | + @NotNull PageBase pageBase, |
| 174 | + @NotNull SmartIntegrationService smartIntegrationService, |
| 175 | + @NotNull QName objectClassName) { |
| 176 | + |
| 177 | + Task task = pageBase.createSimpleTask(OPERATION_REGENERATE_STATISTICS); |
| 178 | + |
| 179 | + try { |
| 180 | + GenericObjectType latestStatistics = |
| 181 | + smartIntegrationService.getLatestStatistics(resourceOid, objectClassName, task.getResult()); |
| 182 | + |
| 183 | + if (latestStatistics == null) { |
| 184 | + pageBase.warn("Statistics computation finished, but no statistics object was found."); |
| 185 | + target.add(pageBase.getFeedbackPanel()); |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + showStatisticsPopup(target, pageBase, latestStatistics, objectClassName); |
| 190 | + |
| 191 | + } catch (CommonException e) { |
| 192 | + pageBase.error("Couldn't load regenerated statistics for " + objectClassName + ": " + e.getMessage()); |
| 193 | + target.add(pageBase.getFeedbackPanel()); |
| 194 | + } finally { |
| 195 | + task.getResult().computeStatusIfUnknown(); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + private @Nullable TaskType loadTask(@NotNull PageBase pageBase, @NotNull String taskOid) { |
| 200 | + Task task = pageBase.createSimpleTask(OP_LOAD_TASK); |
| 201 | + |
| 202 | + PrismObject<TaskType> taskObject = WebModelServiceUtils.loadObject( |
| 203 | + TaskType.class, taskOid, null, true, pageBase, task, task.getResult()); |
| 204 | + |
| 205 | + return taskObject != null ? taskObject.asObjectable() : null; |
| 206 | + } |
| 207 | +} |
0 commit comments