All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.andrewribeiro.ribrest.services.command.GetPersistentChildrenModelCommand Maven / Gradle / Ivy

Go to download

Ribrest Framework - A simple Java framework that truly improve your productivity when developing restful based webservices.

There is a newer version: 1.27.0
Show newest version
package br.com.andrewribeiro.ribrest.services.command;

import br.com.andrewribeiro.ribrest.exceptions.RibrestDefaultException;
import br.com.andrewribeiro.ribrest.model.interfaces.Model;
import br.com.andrewribeiro.ribrest.utils.RibrestUtils;
import java.util.Collection;

/**
 *
 * @author Andrew Ribeiro
 */
public class GetPersistentChildrenModelCommand extends AbstractCommand {

    @Override
    public void execute() throws RibrestDefaultException, Exception {

        Model model = flowContainer.getModel();

        model.getAllCollectionModelAttributes()
                .forEach(attribute -> {
                    try {
                        attribute.setAccessible(true);
                        Collection detachedModels = (Collection) attribute.get(model);
                        Collection newPersistedModels = RibrestUtils.getCollectionInstance(attribute.getType());
                        detachedModels.forEach(detachedModel -> {
                            Object persistedModel = flowContainer.getEntityManager().find(detachedModel.getClass(), ((Model) detachedModel).getId());
                            if (persistedModel == null) {
                                throw new RuntimeException(new StringBuilder("The child model ")
                                        .append(detachedModel.getClass().getSimpleName())
                                        .append(" identified by ")
                                        .append(((Model) detachedModel).getId())
                                        .append(" was not found.").toString());

                            } else {
                                ((Model)persistedModel).merge((Model) detachedModel);
                                newPersistedModels.add(persistedModel);
                            }
                        });
                        
                        attribute.set(model, newPersistedModels);
                    } catch (Exception e) {
                        throw new RuntimeException(e.getMessage());
                    }
                });
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy