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.pojoactor.core.Action;
021import com.scivicslab.pojoactor.core.ActionResult;
022import com.scivicslab.pojoactor.workflow.IIActorRef;
023import com.scivicslab.pojoactor.workflow.IIActorSystem;
024
025import org.yaml.snakeyaml.Yaml;
026
027import java.io.InputStream;
028import java.nio.file.Files;
029import java.nio.file.Path;
030import java.nio.file.Paths;
031import java.util.Map;
032import java.util.logging.Logger;
033
034/**
035 * IIAR wrapper for {@link WorkflowDescriptionSection}.
036 *
037 * <p>Exposes the POJO's methods as actions via {@code @Action} annotations.
038 * Handles workflow path resolution and YAML parsing.</p>
039 *
040 * <h2>Usage in workflow YAML:</h2>
041 * <pre>{@code
042 * - actor: loader
043 *   method: createChild
044 *   arguments: ["reportBuilder", "wfDesc", "...WorkflowDescriptionSectionIIAR"]
045 * }</pre>
046 *
047 * @author devteam@scivicslab.com
048 * @since 2.16.0
049 */
050public class WorkflowDescriptionSectionIIAR extends IIActorRef<WorkflowDescriptionSection> {
051
052    private static final Logger logger = Logger.getLogger(WorkflowDescriptionSectionIIAR.class.getName());
053
054    /**
055     * Constructs the IIAR with a new POJO instance.
056     *
057     * @param actorName the actor name
058     * @param system the actor system
059     */
060    public WorkflowDescriptionSectionIIAR(String actorName, IIActorSystem system) {
061        super(actorName, new WorkflowDescriptionSection(), system);
062        initializeFromWorkflow();
063    }
064
065    /**
066     * Initializes the POJO with workflow information from nodeGroup.
067     */
068    private void initializeFromWorkflow() {
069        String workflowPath = getWorkflowPath();
070        if (workflowPath == null) {
071            return;
072        }
073
074        String description = readDescriptionFromYaml(workflowPath);
075        if (description != null) {
076            object.setDescription(description);
077        }
078    }
079
080    private String getWorkflowPath() {
081        if (actorSystem == null || !(actorSystem instanceof IIActorSystem)) return null;
082        IIActorSystem iiSystem = (IIActorSystem) actorSystem;
083
084        IIActorRef<?> nodeGroup = iiSystem.getIIActor("nodeGroup");
085        if (nodeGroup == null) return null;
086
087        ActionResult result = nodeGroup.callByActionName("getWorkflowPath", "");
088        return result.isSuccess() ? result.getResult() : null;
089    }
090
091    private String readDescriptionFromYaml(String workflowPath) {
092        try {
093            Path path = Paths.get(workflowPath);
094            if (!Files.exists(path)) {
095                path = Paths.get(System.getProperty("user.dir"), workflowPath);
096            }
097
098            if (Files.exists(path)) {
099                try (InputStream is = Files.newInputStream(path)) {
100                    Yaml yaml = new Yaml();
101                    Map<String, Object> data = yaml.load(is);
102                    if (data != null && data.containsKey("description")) {
103                        Object descObj = data.get("description");
104                        return descObj != null ? descObj.toString().trim() : null;
105                    }
106                }
107            }
108        } catch (Exception e) {
109            logger.warning("WorkflowDescriptionSectionIIAR: Could not read workflow file: " + e.getMessage());
110        }
111        return null;
112    }
113
114    // ========================================================================
115    // Actions - expose POJO methods
116    // ========================================================================
117
118    @Action("generate")
119    public ActionResult generate(String args) {
120        String content = object.generate();
121        return new ActionResult(true, content);
122    }
123
124    @Action("getTitle")
125    public ActionResult getTitle(String args) {
126        String title = object.getTitle();
127        return new ActionResult(true, title != null ? title : "");
128    }
129}