Class ModelWurblet

java.lang.Object
org.wurbelizer.wurblet.AbstractWurblet
org.wurbelizer.wurblet.AbstractJavaWurblet
org.tentackle.wurblet.ModelWurblet
All Implemented Interfaces:
org.wurbelizer.wurblet.Wurblet
Direct Known Subclasses:
AttributeNames, ColumnLengths, ColumnNames, DomainMethods, Methods, ModelComment, Relations, UniqueDomainKey

public class ModelWurblet extends org.wurbelizer.wurblet.AbstractJavaWurblet
Extended AbstractJavaWurblet providing basic functionality for the persistent object model.

The following wurblet options are parsed:

  • --method=<method-name>: optional name of the generated method. Defaults to the wurblet tag.
  • --model=<mapping>: optional model mapping. Derived from source file, if missing.
  • --remote: force generating remote code. Taken from model-defaults, if missing.
  • --noremote: don't generate remote code. Taken from model-defaults, if missing.
For the general form of a wurblet-anchor, see AbstractWurblet.
Author:
harald
  • Constructor Details

    • ModelWurblet

      public ModelWurblet()
      Creates a wurblet.
  • Method Details

    • getModelDirName

      public String getModelDirName()
      Gets the name of the model directory.
      Returns:
      the model dir name
    • getModelDefaults

      public ModelDefaults getModelDefaults()
      Gets the optional model defaults.
      Returns:
      the defaults, null if none
    • getPdoClassName

      public String getPdoClassName() throws org.wurbelizer.wurbel.WurbelException
      Gets the pdo/operation class from the source.
      Looks for annotations @DomainObjectService, @PersistentObjectService, @DomainOperationService, @PersistentOperationService and interface extensions.
      Returns:
      the pdo name
      Throws:
      org.wurbelizer.wurbel.WurbelException - if pdo class cannot be determined from the source file
    • isPdo

      public boolean isPdo()
      Returns whether wurblet located within a pdo.
      Returns:
      true for PDOs, false for low-level objects
    • isOperation

      public boolean isOperation()
      Returns whether wurblet located within an operation.
      Returns:
      true if operation
    • isInterface

      public boolean isInterface()
      Returns whether wurblet is defined within an interface.
      Only valid if isPdo() returns non-null.
      Overrides:
      isInterface in class org.wurbelizer.wurblet.AbstractJavaWurblet
      Returns:
      true if interface
    • isRemote

      public boolean isRemote()
      Returns true if --remote option set.
      Returns:
      true if remote
    • setRemote

      public void setRemote(boolean remote)
      Sets the remote option explicitly.
      Parameters:
      remote - true if remoting enabled
    • isPartOfInheritanceHierarchy

      public boolean isPartOfInheritanceHierarchy()
      Returns whether the entity is part of an inheritance tree.
      Returns:
      true if part of an inheritance tree
    • isGenerified

      public boolean isGenerified()
      Returns whether the class is defined using java generics.

      Generics are used in abstract inheritable classes. Final concrete PDO classes must not use generics. Otherwise, the generated wurblet code will not compile.

      Returns:
      true if class is generified
    • isMuteOptionSet

      public boolean isMuteOptionSet(Entity entity)
      Returns whether at least one of the mapped attributes has the [MUTE]-option set.
      Parameters:
      entity - the entity
      Returns:
      true if not all attributes mapped to the Java entity
    • getMethodName

      public String getMethodName() throws org.wurbelizer.wurbel.WurbelException
      Gets the methodname.
      From the guardname or from arg "--method=<.....>" if present.
      Returns:
      the method name
      Throws:
      org.wurbelizer.wurbel.WurbelException - if no guardname
    • getModelName

      public String getModelName() throws org.wurbelizer.wurbel.WurbelException
      Gets the name of the modelfile.
      Returns:
      the name
      Throws:
      org.wurbelizer.wurbel.WurbelException - if model could not be determined
    • deriveClassNameForEntity

      public String deriveClassNameForEntity(Entity otherEntity) throws org.wurbelizer.wurbel.WurbelException
      Applies the semantics of AbstractJavaWurblet.getClassName() to another entity.
      Example:
       getEntity() -> Firma
       getClassName() -> "MyFirmaPersistenceImpl"
       Assumed that otherEntity = Kontakt (which is a superclass of Firma, for example), then:
       deriveClassNameForEntity(otherEntity) -> "MyKontaktPersistenceImpl"
       
      Parameters:
      otherEntity - the other entity
      Returns:
      the derived classname
      Throws:
      org.wurbelizer.wurbel.WurbelException - if this classname does not contain the entity name as a substring
    • orderByInheritanceLevelAndClassId

      public List<Entity> orderByInheritanceLevelAndClassId(List<Entity> entities)
      Sorts the given list of entities by inheritance level plus classid.
      Parameters:
      entities - the entities
      Returns:
      the sorted entities (same reference as argument)
    • getEntity

      public Entity getEntity()
      Gets the model entity.
      Returns:
      the entity
    • getArgs

      public List<String> getArgs()
      Gets the wurblet arguments.
      Returns:
      the wurblet args
    • getOptionArgs

      public List<String> getOptionArgs()
      Gets the wurblet options.
      The options got the leading '--' removed.
      Returns:
      the option args
    • getOption

      public String getOption(String option)
      Gets the option if set.
      Options come in two flavours:
      1. without a value. Example: --remote
      2. with a value. Example: --model=modlog.map
      Parameters:
      option - the option
      Returns:
      the empty string (case 1), the value (case 2) or null if option not set
    • getWurbletArgs

      public List<String> getWurbletArgs()
      Gets the wurblet arguments.
      All arguments except the options.
      Returns:
      the wurblet args
    • createRelationSelectMethodName

      public String createRelationSelectMethodName(Relation relation)
      Creates the method name to select a relation.
      Parameters:
      relation - the relation
      Returns:
      the method name
    • createListRelationDeleteMethodName

      public String createListRelationDeleteMethodName(Relation relation)
      Creates the method name to select a relation.
      Parameters:
      relation - the relation
      Returns:
      the method name
    • createDeclaredArgsForSelectOrDeleteMethod

      public String createDeclaredArgsForSelectOrDeleteMethod(Relation relation) throws ModelException
      Creates the method argument declaration for the select or delete method.
      Parameters:
      relation - the relation
      Returns:
      the argument declaration
      Throws:
      ModelException - if failed
    • getEffectiveDataType

      public org.tentackle.sql.DataType<?> getEffectiveDataType(Attribute attribute) throws org.wurbelizer.wurbel.WurbelException
      Gets the effective datatype.
      The effective type may be different if type is a Convertible.
      Parameters:
      attribute - the attribute
      Returns:
      the data type
      Throws:
      org.wurbelizer.wurbel.WurbelException
    • getColumnName

      public String getColumnName(Attribute attribute, int columnIndex) throws org.wurbelizer.wurbel.WurbelException
      Gets the column name.
      Parameters:
      attribute - the attribute
      columnIndex - the column index, -1 if without suffix
      Returns:
      the column name
      Throws:
      org.wurbelizer.wurbel.WurbelException - if failed
    • getColumnNameConstant

      public String getColumnNameConstant(Attribute attribute, int columnIndex) throws org.wurbelizer.wurbel.WurbelException
      Gets the name of the string constant for a database column name.
      Parameters:
      attribute - the attribute
      columnIndex - the column index within the attribute, -1 if without suffix
      Returns:
      the constant CN_...
      Throws:
      org.wurbelizer.wurbel.WurbelException
    • getAnnotationOptions

      public List<AnnotationOption> getAnnotationOptions(List<String> annotations, String annotationType)
      Returns the annotation options for all annotation strings provided by the model and a given annotation type.
      Parameters:
      annotations - the annotations of the model
      Returns:
      the annotation options matching the annotation type (without package name)
    • getEmbeddedTableAttributes

      public List<Attribute> getEmbeddedTableAttributes() throws org.wurbelizer.wurbel.WurbelException
      Determines all embedded attributes for the table of the entity.
      Returns:
      the list of embedded attributes, empty if none
      Throws:
      org.wurbelizer.wurbel.WurbelException - if failed
    • run

      public void run() throws org.wurbelizer.wurbel.WurbelException

      Overridden to load the map file.

      Specified by:
      run in interface org.wurbelizer.wurblet.Wurblet
      Overrides:
      run in class org.wurbelizer.wurblet.AbstractWurblet
      Throws:
      org.wurbelizer.wurbel.WurbelException - if running the wurblet failed
    • getBackends

      public Collection<org.tentackle.sql.Backend> getBackends()
      Gets the backends that will be used.
      Useful if a feature is used that may not be supported by all backends.
      Returns:
      the backends, never null but may be empty
    • assertSupportedByBackends

      public void assertSupportedByBackends(String feature, Function<org.tentackle.sql.Backend,Boolean> backendValidator) throws org.wurbelizer.wurbel.WurbelException
      Checks that all backends support a given feature.
      The feature validator is a Consumer that must return false or throw a RuntimeException if unsupported.
      Parameters:
      feature - a string describing the feature
      backendValidator - the feature validator
      Throws:
      org.wurbelizer.wurbel.WurbelException - if some validator failed
    • isIdAttribute

      public boolean isIdAttribute(Attribute attribute)
      Checks whether attribute is the pdo ID.
      Parameters:
      attribute - the attribute
      Returns:
      true if pdo id
    • isSerialAttribute

      public boolean isSerialAttribute(Attribute attribute)
      Checks whether attribute is the pdo serial.
      Parameters:
      attribute - the attribute
      Returns:
      true if pdo serial
    • isIdOrSerialAttribute

      public boolean isIdOrSerialAttribute(Attribute attribute)
      Checks whether attribute is the pdo ID or serial.
      Parameters:
      attribute - the attribute
      Returns:
      true if pdo id or serial
    • isAttributeDerived

      public boolean isAttributeDerived(Attribute attribute)
      Checks whether attribute is derived from a superclass.
      Parameters:
      attribute - the attribute
      Returns:
      true if derived from superclass
    • appendCommaSeparated

      public void appendCommaSeparated(StringBuilder builder, String appendStr)
      Adds a string to a comma separated list.
      Parameters:
      builder - the string builder
      appendStr - the string to append
    • prependCommaSeparated

      public void prependCommaSeparated(StringBuilder builder, String prependStr)
      Prepends a string to a comma separated list.
      Parameters:
      builder - the string builder
      prependStr - the string to prepend
    • createComponentInfo

      public ComponentInfo createComponentInfo(Entity component) throws org.wurbelizer.wurbel.WurbelException
      Creates the component info for a given component.
      Parameters:
      component - the component of the wurblet's entity
      Returns:
      the info
      Throws:
      org.wurbelizer.wurbel.WurbelException - if failed
    • createAccessorCode

      public String createAccessorCode(Entity entity, String path, boolean createSetter) throws org.wurbelizer.wurbel.WurbelException
      Creates the java code to access an attribute or column.
      Relations are separated by dots. For multi-column types, the column is separated from the attribute by a hash.
      Parameters:
      entity - the entity where the path begins
      path - the accessor path
      createSetter - true if create setter, else getter
      Returns:
      the java code without the trailing braces
      Throws:
      org.wurbelizer.wurbel.WurbelException - if failed
    • getNonPrimitiveJavaType

      public String getNonPrimitiveJavaType(Attribute attribute) throws ModelException
      Gets the non-primitive java type string of an attribute.
      Parameters:
      attribute - the attribute
      Returns:
      the java type as a string
      Throws:
      ModelException - if type conversion failed
    • determinePackageName

      public String determinePackageName(String className) throws org.wurbelizer.wurbel.WurbelException
      Determines the package name of a class from the source contents.
      This is done by scanning the import statements.
      Rather costly. Use only if there is no other way to determine the package (by rule, for example).
      Parameters:
      className - the simple classname
      Returns:
      the package name (never null)
      Throws:
      org.wurbelizer.wurbel.WurbelException - if package could not be determined