Literal Aspect

Table of Contents

logo-large

Tutorial Actifsource Tutorial – Literal Aspect
Required Time - 30 Minutes
Prerequisites - Actifsource Tutorial – Installing Actifsource
- Actifsource Tutorial – Simple Service
Goal - Writing an aspect for custom literal types
- Validate custom literals
Topics covered - Create a simple demo model
- Add a new Literal
- Implement the Literal Aspect
- Using the Literal Aspect
Notation ↪ To do
ⓘ Information
Bold: Terms from actifsource or other technologies and tools
Bold underlined: actifsource Resources
Monospaced: User input
Italics: Important terms in current situation
Disclaimer The authors do not accept any liability arising out of the application or use of any information or equipment described herein. The information contained within this document is by its very nature incomplete. Therefore the authors accept no responsibility for the precise accuracy of the documentation contained herein. It should be used rather as a guide and starting point.
Contact Actifsource AG
Täfernstrasse 37
5405 Baden-Dättwil
Switzerland
www.actifsource.com
Trademark Actifsource is a registered trademark of Actifsource AG in Switzerland, the EU, USA, and China. other names appearing on the site may be trademarks of their respective owners.

Overview

  • Create a simple demo model
  • Add a new Literal

image2 image2

  • Setup the project

image3 image3

  • Implement the Literal Aspect
  • Using the Literal Aspect

image4 image4

Part I Setup a simple demo model

  • Prepare a new actifsource Project as seen in the Actifsource Tutorial Simple Service with name and namespace ch.actifsource.tutorial.literal.aspect as shown in this tutorial

image5 image5

  • Create a new class as shown in the picture with name Component and one attribute
  • As you can see in the content assist, there is no Literal for representing a date.
  • Create a new Literal using the content assist

image2 image2

  • Set the name to DateLiteral
  • For the moment there is not much more we can do, since we first need to setup the project.

Part II Setup the project

image30 image30

  • First we need to to convert this project to a Eclipse Plugin Project
  • Right click on the project ch.actifsource.tutorial.literal.aspect in the navigator to open the context menu.
  • Go into the submenu Configure and click on Convert to Plug-in Projects…
Info

Note that eclipse allows a project to be an actifsource project and a Eclipse Plugin project at the same time.

image31 image31

  • Check the checkbox in front of ch.actifsource.tutorial.literal.aspect
  • Click Finish

image32 image32

  • This will convert the project however there will be some error and warning needed to be fixed.

image33 image33

  • Right click on the Actifsource classpath container
  • Go into the submenu Build Path and select Remove from Build Path

image34 image34

  • Open the MANIFEST.MF

image35 image35

  • Now you have a Plug-In project and need to set the dependencies to actifsource plugin defining the Literal Aspect. In this case it is the ch.actifsource.core"-Plug-In.
  • Add a dependency to the ch.actifsource.core" and the ch.actifsource.ui.refactoring-Plugin

image36 image36

  • Create a new java source folder for the aspect implementation.
  • We recommend naming the new source folder src
  • Click Finish

Part III Implement the Literal Aspect

  • After the project setup, we are ready to create a java class using an actifsource core interface
  • Java Class implementing the ch.actifsource.tutorial.literal.aspect.ILiteralAspect.

image11 image11

  • There are four methods to implement
  • getValueType The java type representing the literal value. In our case the java “Date” class.
  • getValue A conversion method to convert the literal value to an instance of the value type, which may return “null”, if the value is invalid
  • allowMultiLine True, to allow the use to type in multiple lines using line breaks
  • isValid Returning an error string if the value is invalid or “null” if valid
  • Implement the LiteralAspect as following
package ch.actifsource.tutorial.literal.aspect;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

import javax.annotation.CheckForNull;


import ch.actifsource.core.INode;
import ch.actifsource.core.job.IReadJobExecutor;
import ch.actifsource.core.model.aspects.ILiteralAspect;
import ch.actifsource.core.scope.IResourceScope;

public class DateLiteral implements ILiteralAspect {

  @Override
  public boolean allowMultiline() {
    return false;
  }

  @Override
  public @CheckForNull Object getValue(IReadJobExecutor executor, IResourceScope arg1, INode value) {
    DateFormat dateInstance = DateFormat.getDateInstance(DateFormat.LONG, Locale.ENGLISH);
    try {
      return dateInstance.parse(value.toString());
    } catch (ParseException e) {
      return null;
    }
  }

  @Override
  public Class<?> getValueType() {
    return Date.class;
  }

  @Override
  public @CheckForNull String isValid(IReadJobExecutor executor, IResourceScope scope, String value) {
    DateFormat dateInstance = DateFormat.getDateInstance(DateFormat.LONG, Locale.ENGLISH);
    try {
      dateInstance.parse(value.toString());
      return null;
    } catch (ParseException e) {
      return e.getMessage();
    }
  }

}

Part IV Using the Literal Aspect

  • Go back to the “DateLiteral” and add new value for the aspect [LiteralAspect] relation.

image13 image13

  • Type “Date” and use the content assist to select the class.

image14 image14

  • The Literal is now ready to use, since we created the Literal early with the content assist, the attribute in the “Component” is already using it

image15 image15

image16 image16

  • To test the literal, just create a new instance of the “Component” and define a “releaseDate”.

image4 image4

actifsource-point-large