Class 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 Detail

      • ModelWurblet

        public ModelWurblet()
        Creates a wurblet.
    • Method Detail

      • 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 class from the source.
        Looks for annotations @DomainObjectService, @PersistentObjectService 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 this is a pdo.
        Returns:
        true if pdo
      • isInterface

        public boolean isInterface()
        Returns whether wurblet is defined within an interface.
        Only valid if isPdo() returns non-null.
        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:
        truf 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
      • 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 columname.
        Parameters:
        attribute - the attribute
        columnIndex - the column index, -1 if without suffix
        Returns:
        the columnname
        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
      • 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