×
Back to book

SpringBot Sharing Attributes Across Entities

In some cases, you may wish to have the same attributes used across all of your entities. Using the diagram editor, this would require a multitude of references and/or attributes to be added with the potential for inconsistencies between each of them. There is a better solution through making use of the abstraction within the code.

Client-side

Adding to the View

Attributes can be added to all entities by updating the abstract.model.ts file, which all entities inherit from. This can be found at clientside/src/app/lib/models/abstract.model.ts.

For example, to add a new string attribute called Name to every entity, we would do the following:

  1. Activate the protected region called Add any additional default model properties here in abstract.model.ts.
  2. Add the attribute:
    // % protected region % [Add any additional default model properties here] on begin
     {
         name: 'name',
         displayName: 'Name',
         type: ModelPropertyType.STRING,
     },
     // % protected region % [Add any additional default model properties here] end

You will now have a Name attribute visible on all entities, however, you will not be able to utilise it as it doesn't exist within the persistence layer.

Adding to the Persistence Layer

  1. Activate the protected region called Add any additional class fields here in abstract.model.ts.

  2. Add the following:

     // % protected region % [Add any additional class fields here] on begin
     name: string;
     // % protected region % [Add any additional class fields here] end
  3. Activate the protected region called Add any additional assignments here in abstract.model.ts.

  4. Add the following into it:

     // % protected region % [Add any additional assignments here] on begin
     this.name = json.name;
     // % protected region % [Add any additional assignments here] end
  5. Activate the protected region called Add additional toJSON attributes here in abstract.model.ts.

  6. Add the following into it:

     // % protected region % [Add additional toJSON attributes here] on begin
     name: this.name,
     // % protected region % [Add additional toJSON attributes here] end

    NOTE: This protected region will be released in version 1.2.1.0 of SpringBot.

  7. Open the file clientside/src/app/lib/services/http/abstract-http.service.ts and activate the protected region called Add additional base properties here.

  8. Add the following:

     // % protected region % [Add additional base properties here] on begin
     extraBaseProperties += 'name';
     // % protected region % [Add additional base properties here] end

    NOTE: This protected region will be released in version 1.2.1.0 of SpringBot.

Server-side

Similar to the client-side, we can make use of our AbstractEntity.java to add additional attributes. This file can be found at serverside/src/main/java/[projectName]/entities/AbstractEntity.java

Following the example started in the client-side section, we will achieve this by completing the following steps:

  1. Activate the protected region called Add any additional class methods here in AbstractEntity.java.

  2. Add the attribute using the following code:

     // % protected region % [Add any additional class methods here] on begin
     @ApiModelProperty(notes = "The name.")
     @Column(name = "name")
     private String name;
     // % protected region % [Add any additional class methods here] end

    You will notice that we also add the API documentation to this attribute using the ApiModelProperty annotation. This is important to ensure that our API documentation stays up to date.
    References can be added in a similar manner, but be careful, since all entities inherit from the AbstractEntity you may end up adding a self reference to the source entity.

  3. Open serverside/src/main/resources/graphql/schemas/myuser.schema.graphql and activate the protected region called Add any additional field definition here.

  4. Add the following to it:

     # % protected region % [Add any additional field definition here] on begin
     name: String
     # % protected region % [Add any additional field definition here] end
  5. In the same file, activate the protected region called Add any additional field definition for input type here

  6. Add the following:

     # % protected region % [Add any additional field definition for input type here] on begin
     name: String
     # % protected region % [Add any additional field definition for input type here] end