Support

Expand all | Collapse all

Velocity - loop through item types

  • 1.  Velocity - loop through item types

    Posted 05-03-2018 17:29
    ​I've been trying to figure out how to loop through item types of the current project.  My goal is to list the field names defined for each item type.
    But I can't even get to first base (declaring the class).  I've tried the following but all of these cause the script to fail.
      #set($projectTypes = $applicationContext.getBean("ProjectType"))
      #set($projectTypeFields = $applicationContext.getBean("ProjectTypeField"))
      #set($projectTypes = $applicationContext.getBean("ProjectTypeDao"))
      #set($projectTypeFields = $applicationContext.getBean("ProjectTypeFieldDao"))
      #set($documentTypes = $applicationContext.getBean("DocumentTypeDao"))
      #set($documentNodeManager = $applicationContext.getBean("documentNodeManager"))

    I have 3 questions:
    1. Which class can I use to loop through the item types?
    2. Are all 6 of these non-operational or am I doing something wrong?
    3. If all 6 of these are non-operational then is there a master list of classes that do work?

    Thanks.
    Debbie

    ------------------------------
    Debra Shannon
    Sentek Global
    San Diego CA
    ------------------------------


  • 2.  RE: Velocity - loop through item types

    Posted 05-18-2018 05:45
    Edited by Anuj Sadani 05-20-2018 21:50
    Hi, I guess you can do it via REST API, by looping and recursion

    Try -
    https://dev.jamacloud.com/rest/latest/items?project=204&rootOnly=true&startAt=0&maxResults=50&include=data.itemType

    Or
    call: https://dev.jamacloud.com/rest/latest/itemtypes
    which will have all the itemtypes, then call the abstractitems API for all the itemtypes for the project, if totalResults is 0 the itemtype is not present for the project, if >0 the its available.
    https://dev.jamacloud.com/rest/latest/abstractitems?project=204&itemType=8900

    ------------------------------
    Anuj Sadani
    ------------------------------



  • 3.  RE: Velocity - loop through item types

    Posted 05-18-2018 09:42

    Anuj is correct. Our API uses pagination so either you need to implement a while loop or recursion in order to retrieve all the records for a particular endpoint. 

    The logic to accomplish this is as follows:

    1. initial GET request with the "startAt" parameter set to 0. you can also specify the "maxRecords" with a value of 1-50 (if not specified this will default to 20)

    2. this is where the looping/recursion takes place; look into the response of the previous GET request which will have a "meta.pageinfo" section. this meta information will contain more information on how many more pages you need to retrieve.

    3. Continue to make requests until you retrieve all the data, by procedurally incrementing your "startAt" parameter by your specified "maxRecords" amount. 

    4. Finally, look into the meta.pageInfo for knowing when you need to exit this loop/recursion. 

    Here is also some good coding examples to help illustrate how to accomplish this in various programming languages.

    https://github.com/JamaSoftware/REST-References  


    Also I find it very helpful to use our the swagger documentation when working with the API. This documentation gives you the ability to make actual live requests to your Jama instance, where you can see the actual responses from the API. You can reach this by appending "api-docs" to the URL of your jama instance. (ex. my-jama.jamacloud.com/api-docs).



    ------------------------------
    Chris Asakawa
    ------------------------------



  • 4.  RE: Velocity - loop through item types

    Posted 08-01-2018 14:25
    This the relevant snippet that I ended up with, thanks to help from Jama Software:
    #set($docDao = $applicationContext.getBean("documentDao"))
    #set($contourItemDao = $applicationContext.getBean("contourItemDao"))
    #set($documentTypeFieldDao = $applicationContext.getBean("documentTypeFieldDao"))
    <table>
    #####Get the docs in the project #########
    #set($projectDocIds = $contourItemDao.getContourItemIdsForProject($mathTool.toInteger($project.id)))
    #set($found = 'no')
    #set($docList = []) ##$docList will be an array of arrays
    ## cycle through all of the items in the project
    #foreach ($projectDocId in $projectDocIds)
      #set($baseDoc = $docDao.getDocument($mathTool.toInteger($projectDocId)))
      #foreach ($myDoc in $docList)
         ## Have the fields for this item type already been added to the $docList array?
            #if ($myDoc.get(0) == $baseDoc.documentType.id)
               ## This Item Type has already been seen so move onto the next item
               #set($found = 'yes')
               #break
           #end
       #end ##end of foreach
       #if($found == 'no')
          ## if the item type hasn't already been seen, add the field names to the $docFields array
          #set($docFields = [])
          #foreach ($d in $documentTypeFieldDao.getDocumentTypeFieldListByType($baseDoc.documentType.id))
             #if($docFields.add($d.documentField.name)) #end
          #end
          #if($docList.add($docFields)) #end  ##Add the $docFields array as an entry in the $docList array
       #end
       #set($found = 'no')
    #end
    ## For each item type included in $docList...
    #foreach($item in $docList)
    <tr>
       ## For each field defined for that item type....
       #foreach($fieldName in $item)
          <td>$fieldName</td>  ## print the name of that field
       #end
    </tr>
    #end
    </table>

    ------------------------------
    Debra Shannon
    Sentek Global
    San Diego CA
    ------------------------------



  • 5.  RE: Velocity - loop through item types

    Posted 08-10-2018 17:29
    Debbie,
    Thank you for sharing this with the community. You're amazing!

    ------------------------------
    Dana Medhaug
    Technical Support Engineer
    Jama Software
    ------------------------------