001/*
002 * Copyright 2025 devteam@scivicslab.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing,
011 * software distributed under the License is distributed on an
012 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied.  See the License for the
014 * specific language governing permissions and limitations
015 * under the License.
016 */
017
018package com.scivicslab.actoriac.report.sections.basic;
019
020import com.scivicslab.actoriac.log.DistributedLogStore;
021import com.scivicslab.pojoactor.core.Action;
022import com.scivicslab.pojoactor.core.ActionResult;
023import com.scivicslab.pojoactor.workflow.IIActorRef;
024import com.scivicslab.pojoactor.workflow.IIActorSystem;
025
026import java.sql.Connection;
027import java.util.logging.Logger;
028
029/**
030 * IIAR wrapper for {@link CheckResultsSection}.
031 *
032 * <p>Exposes the POJO's methods as actions via {@code @Action} annotations.
033 * Handles database connection and session ID retrieval from the system.</p>
034 *
035 * <h2>Usage in workflow YAML:</h2>
036 * <pre>{@code
037 * - actor: loader
038 *   method: createChild
039 *   arguments: ["reportBuilder", "checkResults", "...CheckResultsSectionIIAR"]
040 * }</pre>
041 *
042 * <p>The % prefix notation is used in workflows to mark lines for collection:</p>
043 * <pre>
044 * echo "%[OK] Service is running"
045 * echo "%[ERROR] Config file not found"
046 * </pre>
047 *
048 * @author devteam@scivicslab.com
049 * @since 2.16.0
050 */
051public class CheckResultsSectionIIAR extends IIActorRef<CheckResultsSection> {
052
053    private static final Logger logger = Logger.getLogger(CheckResultsSectionIIAR.class.getName());
054
055    /**
056     * Constructs the IIAR with a new POJO instance.
057     *
058     * @param actorName the actor name
059     * @param system the actor system
060     */
061    public CheckResultsSectionIIAR(String actorName, IIActorSystem system) {
062        super(actorName, new CheckResultsSection(), system);
063        initializeFromSystem();
064    }
065
066    /**
067     * Initializes the POJO with database connection and session ID.
068     */
069    private void initializeFromSystem() {
070        // Get database connection from DistributedLogStore
071        DistributedLogStore logStore = DistributedLogStore.getInstance();
072        if (logStore != null) {
073            Connection conn = logStore.getConnection();
074            if (conn != null) {
075                object.setConnection(conn);
076                logger.fine("CheckResultsSectionIIAR: initialized database connection");
077            }
078        }
079
080        // Get session ID from nodeGroup
081        long sessionId = getSessionIdFromNodeGroup();
082        if (sessionId >= 0) {
083            object.setSessionId(sessionId);
084            logger.fine("CheckResultsSectionIIAR: initialized sessionId=" + sessionId);
085        }
086    }
087
088    /**
089     * Retrieves session ID from nodeGroup actor.
090     */
091    private long getSessionIdFromNodeGroup() {
092        if (actorSystem == null || !(actorSystem instanceof IIActorSystem)) {
093            return -1;
094        }
095        IIActorSystem iiSystem = (IIActorSystem) actorSystem;
096
097        IIActorRef<?> nodeGroup = iiSystem.getIIActor("nodeGroup");
098        if (nodeGroup == null) {
099            return -1;
100        }
101
102        ActionResult result = nodeGroup.callByActionName("getSessionId", "");
103        if (result.isSuccess()) {
104            try {
105                return Long.parseLong(result.getResult());
106            } catch (NumberFormatException e) {
107                logger.warning("CheckResultsSectionIIAR: invalid sessionId: " + result.getResult());
108            }
109        }
110        return -1;
111    }
112
113    // ========================================================================
114    // Actions - expose POJO methods
115    // ========================================================================
116
117    @Action("generate")
118    public ActionResult generate(String args) {
119        String content = object.generate();
120        return new ActionResult(true, content);
121    }
122
123    @Action("getTitle")
124    public ActionResult getTitle(String args) {
125        String title = object.getTitle();
126        return new ActionResult(true, title != null ? title : "");
127    }
128}