Override Edit button action

While importing import com.haulmont.cuba.core.app.UniqueNumbersAPI i am getting below error:

The import com.haulmont.cuba.core.app.UniqueNumbersAPI cannot be resolved

same as this topic UniqueNumber API exception where cuba guy suggested to use uniqueNumbersServices instead of API

Ok, well you could implement your own service that basically calls their service. If you use the Studio to create a new service called CustomUniqueNumbersService. Something like this:

Service interface

/**
 * Custom Service Wrapper for UniqueNumbersAPI that provides sequences of unique numbers based on database sequences.
 *
 * @author Vincent 
 */
public interface CustomUniqueNumbersService
{
    /**
     * Service name.
     */
    String NAME = "This will autogenerate during the creation";

    /**
     * Returns the next sequence value.
     *
     * @param domain sequence identifier
     *
     * @return next value
     */
    long getNextNumber(String domain);

    /**
     * Returns the current value of the sequence. For some implementations
     * {@link #getNextNumber(String)} must be called at least once beforehand.
     *
     * @param domain sequence identifier
     *
     * @return current value
     */
    long getCurrentNumber(String domain);

    /**
     * Set current value for the sequence.
     * Next {@link #getCurrentNumber(String)} invocation will return {@code value}
     * Next {@link #getNextNumber(String)} invocation will return {@code value + increment}
     *
     * @param domain sequence identifier
     * @param value  value
     */
    void setCurrentNumber(String domain, long value);

    /**
     * Removes sequence with specified identifier
     *
     * @param domain sequence identifier
     *
     * @throws java.lang.IllegalStateException if sequence does not exist
     */
    void deleteSequence(String domain);
}

Bean

package com.rx30.ems.basicworkflowmanager.service;

import javax.inject.Inject;

import com.haulmont.cuba.core.app.UniqueNumbersAPI;
import org.springframework.stereotype.Service;

/**
 * Custom Service Wrapper for UniqueNumbersAPI that provides sequences of unique numbers based on database sequences.
 *
 * @author Vincent
 */
@Service(CustomUniqueNumbersService.NAME)
public class CustomUniqueNumbersServiceBean implements CustomUniqueNumbersService
{

    @Inject
    protected UniqueNumbersAPI uniqueNumbersAPI;

    /**
     * Returns the next sequence value.
     *
     * @param domain sequence identifier
     *
     * @return next value
     */
    @Override
    public long getNextNumber(String domain)
    {
        return this.uniqueNumbersAPI.getNextNumber(domain);
    }

    /**
     * Returns the current value of the sequence. For some implementations
     * {@link #getNextNumber(String)} must be called at least once beforehand.
     *
     * @param domain sequence identifier
     *
     * @return current value
     */
    @Override
    public long getCurrentNumber(String domain)
    {
        return this.getCurrentNumber(domain);
    }

    /**
     * Set current value for the sequence.
     * Next {@link #getCurrentNumber(String)} invocation will return {@code value}
     * Next {@link #getNextNumber(String)} invocation will return {@code value + increment}
     *
     * @param domain sequence identifier
     * @param value  value
     */
    @Override
    public void setCurrentNumber(String domain, long value)
    {
        this.uniqueNumbersAPI.setCurrentNumber(domain, value);
    }

    /**
     * Removes sequence with specified identifier
     *
     * @param domain sequence identifier
     *
     * @throws IllegalStateException if sequence does not exist
     */
    @Override
    public void deleteSequence(String domain)
    {
        this.uniqueNumbersAPI.deleteSequence(domain);
    }
}

You can use this to do your unique numbers directly in the controller. Now, this may be considered bad form by some because you are bringing code that was not intended to be used outside of the core module to the web module - but you have to make the decision of whether it is bad for you. It should theoretically solve your problem, but just be aware there could be unintended consequences.

The other option would be to build a service for this specific entity that handles just this increment / decrement process and not wrap the whole service.

1 Like

Thank You very much :slight_smile: I hope this may end up in a working phase.

Well, if not, we are here to help - just send a note back and we will take it from there. Good luck!

Hi everyone,

Does anyone knows what happens in concurrent setup? Is it possible that on “Cancel” action setting the sequence number to the previous one lead to collision because other user created a new record?

Thanks for the help.

Sammy

Hi @sambill,

Reverting sequence is not the best way, as it could happen that this sequence has been triggered concurrently by another user. There is only one recommendation, pool a value from the sequence in EntityChangedEvent (EntityChangedEvent - CUBA Platform. Developer’s Manual) at the before commit phase.

Regards,
Aleksey