Class FormatCodeBlocks

java.lang.Object
no.sikt.graphitron.generators.codebuilding.FormatCodeBlocks

public class FormatCodeBlocks extends Object
Class containing various helper methods for constructing code with javapoet.
  • Constructor Details

    • FormatCodeBlocks

      public FormatCodeBlocks()
  • Method Details

    • declareRecord

      public static no.sikt.graphitron.javapoet.CodeBlock declareRecord(String name, RecordObjectSpecification<?> input, boolean isIterable, boolean isResolver)
      Parameters:
      name - Name of a field that should be declared as a record. This will be the name of the variable.
      input - Input type that should be declared as a record.
      isIterable - Is this record wrapped in a list?
      isResolver - Is this declaration to be used in a resolver?
      Returns:
      CodeBlock that declares a new record variable and that attaches context configuration if needed.
    • recordTransformPart

      public static no.sikt.graphitron.javapoet.CodeBlock recordTransformPart(String transformerName, String varName, String typeName, boolean isJava, boolean isInput)
    • ifNotNull

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock ifNotNull(String name)
      Returns:
      CodeBlock that contains an if statement with a null check on the provided name.
    • listOf

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock listOf()
      Returns:
      CodeBlock that creates an empty List.
    • listOf

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock listOf(no.sikt.graphitron.javapoet.CodeBlock code)
      Returns:
      CodeBlock that wraps the supplied CodeBlock in a List.
    • listOf

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock listOf(String variable)
      Returns:
      CodeBlock that wraps the supplied variable name in a List.
    • listOfIf

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock listOfIf(no.sikt.graphitron.javapoet.CodeBlock code, boolean condition)
      Returns:
      CodeBlock that wraps the provided CodeBlock in a Java list provided the condition is true.
    • setOf

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock setOf()
      Returns:
      CodeBlock that creates an empty Set.
    • setOf

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock setOf(no.sikt.graphitron.javapoet.CodeBlock code)
      Returns:
      CodeBlock that wraps the supplied CodeBlock in a Set.
    • setOf

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock setOf(String variable)
      Returns:
      CodeBlock that wraps the supplied variable name in a Set.
    • mapOf

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock mapOf()
      Returns:
      CodeBlock that creates an empty Map.
    • asMethodCall

      public static no.sikt.graphitron.javapoet.CodeBlock asMethodCall(String method)
      Returns:
      CodeBlock that wraps this method name in a method call format.
    • asMethodCall

      public static no.sikt.graphitron.javapoet.CodeBlock asMethodCall(String source, String method)
      Returns:
      CodeBlock that wraps this method name in a method call format after the specified source.
    • asMethodCall

      public static no.sikt.graphitron.javapoet.CodeBlock asMethodCall(no.sikt.graphitron.javapoet.TypeName source, String method)
      Returns:
      CodeBlock that wraps this method name in a static method call format after the specified source.
    • asCast

      public static no.sikt.graphitron.javapoet.CodeBlock asCast(no.sikt.graphitron.javapoet.TypeName type, String variable)
      Returns:
      CodeBlock that wraps this variable in a Java cast.
    • asCast

      public static no.sikt.graphitron.javapoet.CodeBlock asCast(no.sikt.graphitron.javapoet.TypeName type, no.sikt.graphitron.javapoet.CodeBlock code)
      Returns:
      CodeBlock that wraps this code in a Java cast.
    • collectToList

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock collectToList()
      Returns:
      CodeBlock that adds a collect to List call to be used on a Stream.
    • findFirst

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock findFirst()
      Returns:
      CodeBlock that adds a findFirst call to be used on a collection.
    • nullIfNullElse

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock nullIfNullElse(no.sikt.graphitron.javapoet.CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock name in a simple null check.
    • nullIfNullElseThis

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock nullIfNullElseThis(no.sikt.graphitron.javapoet.CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock name in a simple null check.
    • listedNullCheck

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock listedNullCheck(String variable, no.sikt.graphitron.javapoet.CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock name in a mapping null check.
    • addStringIfNotEmpty

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock addStringIfNotEmpty(String target, String addition)
      Returns:
      CodeBlock that adds something to a String if it is not empty.
    • selectionSetLookup

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock selectionSetLookup(String path, boolean atResolver, boolean useArguments)
      Returns:
      CodeBlock that checks whether a path is in use.
    • setValue

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock setValue(String container, MethodMapping mapping, no.sikt.graphitron.javapoet.CodeBlock value, boolean isResolverKey)
      Returns:
      CodeBlock that sets a value through a mapping.
    • setValue

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock setValue(String container, MethodMapping mapping, no.sikt.graphitron.javapoet.CodeBlock value)
      Returns:
      CodeBlock that sets a value through a mapping.
    • getValue

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock getValue(String container, MethodMapping mapping)
      Returns:
      CodeBlock that gets a value through a mapping.
    • newDataFetcher

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock newDataFetcher()
      Returns:
      CodeBlock that creates a data fetcher object.
    • newServiceDataFetcherWithTransform

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock newServiceDataFetcherWithTransform()
      Returns:
      CodeBlock that creates a service data fetcher through a transform object.
    • declareTransform

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock declareTransform()
      Returns:
      CodeBlock that declares a resolver transformer.
    • continueCheck

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock continueCheck(String value)
      Returns:
      CodeBlock does a null check on the variable and runs continue if it is.
    • countFunction

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock countFunction(String queryLocation, String queryMethodName, List<String> inputList, boolean isService)
      Returns:
      CodeBlock consisting of a function for a count DB call.
    • getQueryClassName

      public static no.sikt.graphitron.javapoet.ClassName getQueryClassName(String queryLocation)
    • queryFunction

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock queryFunction(String queryLocation, String queryMethodName, List<String> inputList, boolean hasKeyValues, boolean usesKeyValues, boolean isService)
      Returns:
      CodeBlock consisting of a function for a generic DB call.
    • invokeExternalMethod

      public static no.sikt.graphitron.javapoet.CodeBlock invokeExternalMethod(no.sikt.graphitron.javapoet.CodeBlock source, String methodName, String parameters)
    • declarePageSize

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock declarePageSize(int defaultFirst)
      Returns:
      CodeBlock consisting of a declaration of the page size variable through a method call.
    • wrapNotNull

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock wrapNotNull(String valueToCheck, no.sikt.graphitron.javapoet.CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock in an if not null check.
    • wrapFor

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock wrapFor(String variable, no.sikt.graphitron.javapoet.CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock in a for loop.
    • wrapForIndexed

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock wrapForIndexed(String variable, no.sikt.graphitron.javapoet.CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock in an indexed for loop.
    • wrapRow

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock wrapRow(no.sikt.graphitron.javapoet.CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock in a jOOQ row.
    • wrapObjectRow

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock wrapObjectRow(no.sikt.graphitron.javapoet.CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock in a jOOQ row.
    • wrapCoalesce

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock wrapCoalesce(no.sikt.graphitron.javapoet.CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock in a jOOQ coalesce.
    • inline

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock inline(no.sikt.graphitron.javapoet.CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock in a jOOQ inline.
    • val

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock val(no.sikt.graphitron.javapoet.CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock in a jOOQ val.
    • defaultValue

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock defaultValue(no.sikt.graphitron.javapoet.CodeBlock field)
    • defaultValue

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock defaultValue(String tableName, String fieldName)
    • tableFieldCodeBlock

      public static no.sikt.graphitron.javapoet.CodeBlock tableFieldCodeBlock(String targetTable, String column)
    • tableFieldCodeBlock

      public static no.sikt.graphitron.javapoet.CodeBlock tableFieldCodeBlock(no.sikt.graphitron.javapoet.CodeBlock targetTable, String column)
    • indentIfMultiline

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock indentIfMultiline(no.sikt.graphitron.javapoet.CodeBlock code)
      Returns:
      Add appropriate indentation if this code has multiple lines.
    • makeEnumMapBlock

      public static no.sikt.graphitron.javapoet.CodeBlock makeEnumMapBlock(no.sikt.graphitron.javapoet.CodeBlock inputVariable, no.sikt.graphitron.javapoet.CodeBlock valueLists)
      Returns:
      CodeBlock that sends this variable through an enum mapping.
    • makeEnumMapBlock

      public static no.sikt.graphitron.javapoet.CodeBlock makeEnumMapBlock(String inputVariable, no.sikt.graphitron.javapoet.CodeBlock valueLists)
      Returns:
      CodeBlock that sends this variable through an enum mapping.
    • toJOOQEnumConverter

      public static no.sikt.graphitron.javapoet.CodeBlock toJOOQEnumConverter(String enumType, ProcessedSchema schema)
      Returns:
      Code block containing the enum conversion method call with anonymous function declarations.
    • toGraphEnumConverter

      public static no.sikt.graphitron.javapoet.CodeBlock toGraphEnumConverter(String enumType, no.sikt.graphitron.javapoet.CodeBlock field, boolean toRecord, ProcessedSchema schema)
      Returns:
      Code block containing the enum conversion method call.
    • trueCondition

      public static no.sikt.graphitron.javapoet.CodeBlock trueCondition()
      Returns:
      Code block of a true jOOQ condition.
    • falseCondition

      public static no.sikt.graphitron.javapoet.CodeBlock falseCondition()
      Returns:
      Code block of a false jOOQ condition.
    • noCondition

      public static no.sikt.graphitron.javapoet.CodeBlock noCondition()
      Returns:
      Code block of a jOOQ "no condition".
    • applyGlobalTransforms

      public static no.sikt.graphitron.javapoet.CodeBlock applyGlobalTransforms(String recordName, no.sikt.graphitron.javapoet.TypeName recordTypeName, TransformScope scope)
      Parameters:
      recordName - Name of the record to transform.
      scope - The scope of transforms that should be applied. Currently only TransformScope.ALL_MUTATIONS is supported.
      Returns:
      CodeBlock where all defined global transforms are applied to the record.
    • applyTransform

      public static no.sikt.graphitron.javapoet.CodeBlock applyTransform(String recordName, no.sikt.graphitron.javapoet.TypeName recordTypeName, Method transform)
      Parameters:
      recordName - Name of the record to transform.
      transform - The method that should transform the record.
      Returns:
      CodeBlock where the transform is applied to the record.
    • fetchMapping

      public static no.sikt.graphitron.javapoet.CodeBlock fetchMapping(boolean iterable)
    • returnWrap

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock returnWrap(String variable)
      Returns:
      CodeBlock that returns the provided name.
    • returnWrap

      @NotNull public static @NotNull no.sikt.graphitron.javapoet.CodeBlock returnWrap(no.sikt.graphitron.javapoet.CodeBlock code)
      Returns:
      CodeBlock that returns the provided code.
    • inResolverKeysBlock

      public static no.sikt.graphitron.javapoet.CodeBlock inResolverKeysBlock(String resolverKeyParamName, FetchContext context)
    • getSelectKeyColumnRow

      public static no.sikt.graphitron.javapoet.CodeBlock getSelectKeyColumnRow(org.jooq.Key<?> key, String tableName, String aliasVariableName)
      Returns the select code for the columns of a key.
      Parameters:
      key - The key
      aliasVariableName - The variable name for the table alias
      Returns:
      Select code for the columns in the key
    • getSelectKeyColumnRow

      public static no.sikt.graphitron.javapoet.CodeBlock getSelectKeyColumnRow(FetchContext context)
      Returns codeblock for selecting key columns for the resolver key
      Parameters:
      context - The fetching context
      Returns:
      Select code for the columns in the resolver key
    • getSelectKeyColumn

      public static no.sikt.graphitron.javapoet.CodeBlock getSelectKeyColumn(org.jooq.Key<?> key, String tableName, String aliasVariableName)
    • getSelectKeyColumn

      public static no.sikt.graphitron.javapoet.CodeBlock getSelectKeyColumn(FetchContext context)
    • createNodeIdBlock

      public static no.sikt.graphitron.javapoet.CodeBlock createNodeIdBlock(RecordObjectSpecification<?> obj, String targetAlias)
    • createNodeIdBlockForRecord

      public static no.sikt.graphitron.javapoet.CodeBlock createNodeIdBlockForRecord(RecordObjectSpecification<?> obj, String recordVariableName)
    • hasIdBlock

      public static no.sikt.graphitron.javapoet.CodeBlock hasIdBlock(no.sikt.graphitron.javapoet.CodeBlock id, RecordObjectSpecification<?> obj, String targetAlias)
    • hasIdOrIdsBlock

      public static no.sikt.graphitron.javapoet.CodeBlock hasIdOrIdsBlock(no.sikt.graphitron.javapoet.CodeBlock idOrRecordParamName, RecordObjectSpecification<?> obj, String targetAlias, no.sikt.graphitron.javapoet.CodeBlock mappedFkFields, boolean isMultiple)
    • nodeIdColumnsBlock

      public static no.sikt.graphitron.javapoet.CodeBlock nodeIdColumnsBlock(RecordObjectSpecification<?> obj)
    • referenceNodeIdColumnsBlock

      public static no.sikt.graphitron.javapoet.CodeBlock referenceNodeIdColumnsBlock(RecordObjectSpecification<?> container, RecordObjectSpecification<?> target, org.jooq.ForeignKey<?,?> fk)
    • referenceNodeIdColumnsBlock

      public static no.sikt.graphitron.javapoet.CodeBlock referenceNodeIdColumnsBlock(RecordObjectSpecification<?> container, RecordObjectSpecification<?> target, org.jooq.ForeignKey<?,?> fk, no.sikt.graphitron.javapoet.CodeBlock tableReference)
    • getReferenceNodeIdFields

      public static LinkedList<String> getReferenceNodeIdFields(String targetTable, RecordObjectSpecification<?> targetNodeType, org.jooq.ForeignKey<?,?> fk)
    • nodeIdColumnsWithAliasBlock

      public static no.sikt.graphitron.javapoet.CodeBlock nodeIdColumnsWithAliasBlock(String targetAlias, RecordObjectSpecification<?> obj)
    • getPrimaryKeyFieldsWithTableAliasBlock

      public static no.sikt.graphitron.javapoet.CodeBlock getPrimaryKeyFieldsWithTableAliasBlock(String targetAlias)
    • ofTernary

      public static no.sikt.graphitron.javapoet.CodeBlock ofTernary(no.sikt.graphitron.javapoet.CodeBlock ifExpr, no.sikt.graphitron.javapoet.CodeBlock thenExpr, no.sikt.graphitron.javapoet.CodeBlock elseExpr)