find defined length of a string

0
Hi All Java guru's out there.   Can someone post some code on how we can make a Java action that will return the defined length of a string variable from the entity in the domain model? I'm not looking for the length of what's in it, I am looking for a returned integer that says say 200 (which is one of the most common default string lengths), or 15, if that is what the creator of the string attribute set in the entity.
asked
2 answers
1

Hi Ron,

create a java action with name GetLength with following config

image.png

 

image.png

 

use this code

 

// This file was generated by Mendix Studio Pro.

//

// WARNING: Only the following code will be retained when actions are regenerated:

// - the import list

// - the code between BEGIN USER CODE and END USER CODE

// - the code between BEGIN EXTRA CODE and END EXTRA CODE

// Other code you write will be lost the next time you deploy the project.

// Special characters, e.g., é, ö, à, etc. are supported in comments.

 

package commentsmodule.actions;

 

import com.mendix.systemwideinterfaces.core.IContext;

import com.mendix.webui.CustomJavaAction;

import com.mendix.systemwideinterfaces.core.IMendixObject;

import com.mendix.systemwideinterfaces.core.meta.IMetaObject;

import com.mendix.systemwideinterfaces.core.meta.IMetaPrimitive;

import com.mendix.systemwideinterfaces.core.meta.IMetaPrimitive.PrimitiveType;

import java.util.List;

import java.util.stream.Collectors;

 

public class GetLength extends CustomJavaAction<java.lang.String>

{

    private IMendixObject Entity;

 

    public GetLength(IContext context, IMendixObject Entity)

    {

        super(context);

        this.Entity = Entity;

    }

 

    @java.lang.Override

    public java.lang.String executeAction() throws Exception

    {

        // BEGIN USER CODE

        IContext context = getContext();

        IMetaObject metaObject = Entity.getMetaObject();

       

        List<String> attributesInfo = metaObject.getMetaPrimitives().stream()

            .filter(metaPrimitive -> metaPrimitive.getType() == PrimitiveType.String)

            .map(metaPrimitive -> {

                String attributeName = metaPrimitive.getName();

                PrimitiveType attributeType = metaPrimitive.getType();

                String typeName = attributeType.toString();

                int attributeLength = getDefinedLength(metaPrimitive);

                return String.format("%s-%d", attributeName, attributeLength);

            })

            .collect(Collectors.toList());

        return String.join("\n", attributesInfo);

        // END USER CODE

    }

 

    /**

     * Returns a string representation of this action

     * @return a string representation of this action

     */

    @java.lang.Override

    public java.lang.String toString()

    {

        return "GetLength";

    }

 

    // BEGIN EXTRA CODE

    private int getDefinedLength(IMetaPrimitive metaPrimitive) {

        return metaPrimitive.getLength();

    }

    // END EXTRA CODE

}

 

 

image.png

 

Hope it helps!

answered
0

You should be able to write a java action that uses this method. 

 

 https://apidocs.rnd.mendix.com/9/runtime/com/mendix/systemwideinterfaces/core/meta/IMetaPrimitive.html#getLength()

 

Take a look at the IMetaObject, you can use this interface to get all sorts of info about a specific entity

 

https://apidocs.rnd.mendix.com/9/runtime/com/mendix/systemwideinterfaces/core/meta/IMetaObject.html

 

If you don't want to write your own java action the Model Reflection Module already has this info available 

 

https://marketplace.mendix.com/link/component/69

 

image.png

 

answered