nagve01

CA PPM : Copy ToDo from template to Project Using Rest API

Blog Post created by nagve01 Employee on May 2, 2018

CA PPM Blueprints or templates have todo's in Task. Todos are not populating under the tasks When the user is creating a project using template/ bluprint .

In this case we can use REST API to  copy over the todo from template to project. We have written process which is triggering on the basis of project creation when using a template.

 

Best Practices .

1. Keep maintain External id's (Autonmbering) for the task in  tampletes.

 

Sample Code :

 

<!-- Start: Get the TODO data from template and find the matching task on the project -->
     <sql:query var="cresult" >
     <![CDATA[
      select ST.prid source_task_id, ST.prexternalid source_externalid, DT.prid dest_task_id, DT.prexternalid dest_externalid,
      TD.name source_todo_name, TD.position source_todo_position, TD.completed source_todo_completed,
      sysdate created_date, 1 created_by, sysdate last_updated_date, 1 last_updated_by, DXT.partition_code  
      from prtask ST
      inner join odf_ca_task XT on ST.prid = XT.id
      inner join prtask DT on ST.prexternalid = DT.prexternalid
      inner join prTODO TD on ST.prid = TD.prtaskid
      inner join odf_ca_task DXT on DT.prid = DXT.id
      where 1 = 1
      and ST.prprojectid = ${v_template_int_id}
      and DT.prprojectid = ${gel_objectInstanceId}
      order by ST.prexternalid
     ]]></sql:query>
     
     <core:forEach trim="true" items="${cresult.rowsByIndex}" var="crow">

     <core:set value="${crow[i+0]}" var="source_task_id"/>
      <core:set value="${crow[i+1]}" var="v_source_externalid"/>
      <core:set value="${crow[i+2]}" var="v_dest_task_id"/>
      <core:set value="${crow[i+3]}" var="v_dest_externalid"/>
      <core:set value="${crow[i+4]}" var="v_source_todo_name"/>
      <core:set value="${crow[i+5]}" var="v_source_todo_position"/>
      <core:set value="${crow[i+6]}" var="v_source_todo_completed"/>
      <core:set value="${crow[i+7]}" var="v_created_date"/>
      <core:set value="${crow[i+8]}" var="v_created_by"/>
      <core:set value="${crow[i+9]}" var="v_last_updated_date"/>
      <core:set value="${crow[i+10]}" var="v_last_updated_by"/>    
      <core:set value="${crow[i+11]}" var="v_partition_code"/>
      
      <gel:log level="info" message=" ${v_source_todo_name} Destination task :${v_dest_task_id}"/>      


     <core:new className="sun.misc.BASE64Encoder" var="encoder"/>
     <core:set value="${username}:${password}" var="credentials"/>
     <core:set value="${encoder.encode( credentials.getBytes() )}" var="encodedCredentials"/>   
     <core:set var="httpRESTURL">${XogUrl}/ppm/rest/v1/projects/${v_prj_int_id}/tasks/${v_dest_task_id}/todos</core:set>
     
      <gel:log category="REST" level="INFO" message=" ${httpRESTURL}"/>
     
     <core:set var="payLoad">
       {
        "name":"${v_source_todo_name}",
        "completed":false
       }
     </core:set>
     <core:new className="java.net.URL" var="remoteURL">
      <core:arg type="java.lang.String" value="${httpRESTURL}"/>
     </core:new>
     <core:set value="${remoteURL.openConnection()}" var="connection"/>
     <core:set value="${connection.setDoInput(true)}" var="void"/>
     <core:set value="${connection.setDoOutput(true)}" var="void"/>
     <core:set value="${connection.setUseCaches(false)}" var="void"/>
     <core:invoke method="setRequestMethod" on="${connection}">
      <core:arg type="java.lang.String" value="POST"/>
     </core:invoke>
     <core:invoke method="setRequestProperty" on="${connection}">
      <core:arg type="java.lang.String" value="Content-Type"/>
      <core:arg type="java.lang.String" value="application/json"/>
     </core:invoke>
     <core:invoke method="setRequestProperty" on="${connection}">
      <core:arg type="java.lang.String" value="Accept"/>
      <core:arg type="java.lang.String" value="application/json"/>
     </core:invoke>
     <core:invoke method="setRequestProperty" on="${connection}">
      <core:arg type="java.lang.String" value="Authorization"/>
      <core:arg type="java.lang.String" value="Basic ${encodedCredentials}"/>
     </core:invoke>
     <core:set value="${connection.connect()}" var="void"/>
     <core:new className="java.io.OutputStreamWriter" var="wr">
      <core:arg type="java.io.OutputStream" value="${connection.getOutputStream()}"/>
     </core:new>
     <core:set value="${wr.write(payLoad)}" var="void"/>
     <core:set value="${wr.flush()}" var="void"/>
     <core:set value="${wr.close()}" var="void"/>
     <core:set value="${connection.getHeaderField(0)}" var="isAuthenticated"/>
     <core:set value="${connection.getResponseMessage()}" var="response_msg"/>
     <core:if test="${bDebug}">
      <gel:log category="Authentication Message" level="INFO" message=" ${isAuthenticated}"/>
     </core:if>

     <core:choose>
      <core:when test="${(isAuthenticated != 'HTTP/1.1 200')}">
       
        <gel:log level="WARN" message="  Error creating ToDo- ${isAuthenticated}"/>
       
      </core:when>
      <core:otherwise>
       <core:set value="${connection.getInputStream()}" var="httpInputStream"/>
       <core:new className="java.io.InputStreamReader" var="v_Input">
        <core:arg type="java.io.InputStream" value="${httpInputStream}"/>
       </core:new>
       <core:new className="java.io.BufferedReader" var="v_InputData">
        <core:arg type="java.io.InputStreamReader" value="${v_Input}"/>
       </core:new>
       <core:set value="${v_InputData.readLine()}" var="totalIssues"/>
       <core:if test="${bDebugVerbose}">
        <gel:log category="JSON" level="INFO" message=" ${totalIssues}"/>
       </core:if>
       <core:set value="${httpInputStream.close()}" var="dummy"/>
       <core:new className="org.json.JSONObject" var="jarrtotIssues">
        <core:arg type="java.lang.String" value="${totalIssues}"/>
       </core:new>
       <core:set value="${jarrtotIssues.get('_internalId')}" var="v_toDoInternalId"/>
       <gel:log> ${v_toDoInternalId} </gel:log>
      </core:otherwise>
     </core:choose>


     </core:forEach>
<!-- End: Get the TODO data from the template and find the matching task on the project -->

    </core:if>

   
     </core:when>
<core:otherwise>
<gel:log  level="INFO">Template is not not found </gel:log>
</core:otherwise>

Outcomes