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 WorkflowNameSection}. 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", "wfName", "...WorkflowNameSectionIIAR"] 045 * }</pre> 046 * 047 * @author devteam@scivicslab.com 048 * @since 2.16.0 049 */ 050public class WorkflowNameSectionIIAR extends IIActorRef<WorkflowNameSection> { 051 052 private static final Logger logger = Logger.getLogger(WorkflowNameSectionIIAR.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 WorkflowNameSectionIIAR(String actorName, IIActorSystem system) { 061 super(actorName, new WorkflowNameSection(), 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 object.setWorkflowPath(workflowPath); 075 076 // Try to read name from YAML 077 String name = readNameFromYaml(workflowPath); 078 if (name != null) { 079 object.setWorkflowName(name); 080 } 081 } 082 083 private String getWorkflowPath() { 084 if (actorSystem == null || !(actorSystem instanceof IIActorSystem)) return null; 085 IIActorSystem iiSystem = (IIActorSystem) actorSystem; 086 087 IIActorRef<?> nodeGroup = iiSystem.getIIActor("nodeGroup"); 088 if (nodeGroup == null) return null; 089 090 ActionResult result = nodeGroup.callByActionName("getWorkflowPath", ""); 091 return result.isSuccess() ? result.getResult() : null; 092 } 093 094 private String readNameFromYaml(String workflowPath) { 095 try { 096 Path path = Paths.get(workflowPath); 097 if (!Files.exists(path)) { 098 path = Paths.get(System.getProperty("user.dir"), workflowPath); 099 } 100 101 if (Files.exists(path)) { 102 try (InputStream is = Files.newInputStream(path)) { 103 Yaml yaml = new Yaml(); 104 Map<String, Object> data = yaml.load(is); 105 if (data != null && data.containsKey("name")) { 106 return (String) data.get("name"); 107 } 108 } 109 } 110 } catch (Exception e) { 111 logger.warning("WorkflowNameSectionIIAR: Could not read workflow file: " + e.getMessage()); 112 } 113 return null; 114 } 115 116 // ======================================================================== 117 // Actions - expose POJO methods 118 // ======================================================================== 119 120 @Action("generate") 121 public ActionResult generate(String args) { 122 String content = object.generate(); 123 return new ActionResult(true, content); 124 } 125 126 @Action("getTitle") 127 public ActionResult getTitle(String args) { 128 String title = object.getTitle(); 129 return new ActionResult(true, title != null ? title : ""); 130 } 131}