Search in sources :

Example 11 with BusinessException

use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.

the class CustomEntityInstanceService method addFilesToModule.

@Override
public void addFilesToModule(CustomEntityInstance entity, MeveoModule module) throws BusinessException {
    String cetCode = entity.getCetCode();
    String ceiJson = CEIUtils.serialize(entity);
    File gitDirectory = GitHelper.getRepositoryDir(currentUser, module.getCode());
    String path = entity.getClass().getAnnotation(ModuleItem.class).path() + "/" + cetCode;
    File newDir = new File(gitDirectory, path);
    newDir.mkdirs();
    File newJsonFile = new File(gitDirectory, path + "/" + entity.getUuid() + ".json");
    try {
        MeveoFileUtils.writeAndPreserveCharset(ceiJson, newJsonFile);
    } catch (IOException e) {
        throw new BusinessException("File cannot be updated or created", e);
    }
    gitClient.commitFiles(module.getGitRepository(), Collections.singletonList(newDir), "Add JSON file for entity " + entity.getCode());
}
Also used : ModuleItem(org.meveo.model.ModuleItem) BusinessException(org.meveo.admin.exception.BusinessException) IOException(java.io.IOException) File(java.io.File)

Example 12 with BusinessException

use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.

the class CustomEntityInstanceService method transitionsFromPreviousState.

public boolean transitionsFromPreviousState(String cftCode, CustomEntityInstance instance) throws ELException {
    Workflow workflow = workflowService.findByCetCodeAndWFType(instance.getCetCode(), cftCode);
    if (workflow != null) {
        List<WFTransition> transitions = new ArrayList<>();
        List<String> statusWF = new ArrayList<>();
        List<WFTransition> wfTransitions = workflow.getTransitions();
        if (CollectionUtils.isNotEmpty(wfTransitions)) {
            for (WFTransition wfTransition : wfTransitions) {
                wfTransition = wfTransitionService.findById(wfTransition.getId());
                boolean isTransitionApplicable = MeveoValueExpressionWrapper.evaluateToBooleanOneVariable(wfTransition.getConditionEl(), "entity", instance);
                String targetStatus = instance.getCfValues().getValuesByCode().get(cftCode).get(0).getStringValue();
                String startStatus = (String) instance.getCfValuesOldNullSafe().getValue(cftCode);
                boolean isSameTargetStatus = wfTransition.getToStatus().equals(targetStatus);
                boolean isSameStartStatus = wfTransition.getFromStatus().equals(startStatus);
                if (isTransitionApplicable && isSameTargetStatus && isSameStartStatus) {
                    transitions.add(wfTransition);
                    statusWF.add(wfTransition.getToStatus());
                }
            }
        }
        if (CollectionUtils.isEmpty(transitions)) {
            log.debug("Update refused because no transition matched");
            return false;
        }
        for (WFTransition wfTransition : transitions) {
            if (CollectionUtils.isNotEmpty(wfTransition.getWfActions())) {
                for (WFAction action : wfTransition.getWfActions()) {
                    WFAction wfAction = wfActionService.findById(action.getId());
                    if (action.getConditionEl() == null || MeveoValueExpressionWrapper.evaluateToBooleanOneVariable(action.getConditionEl(), "entity", instance)) {
                        Object actionResult;
                        if (wfAction.getActionScript() != null) {
                            try {
                                actionResult = workflowService.executeActionScript(instance, wfAction);
                            } catch (BusinessException e) {
                                log.error("Error execution workflow action script", e);
                            }
                        } else if (StringUtils.isNotBlank(wfAction.getActionEl())) {
                            actionResult = workflowService.executeExpression(wfAction.getActionEl(), instance);
                        } else {
                            log.error("WFAction {} has no action EL or action script", wfAction.getId());
                            continue;
                        }
                    // TODO: Log action result ?
                    }
                }
            }
        }
    }
    return true;
}
Also used : WFTransition(org.meveo.model.wf.WFTransition) BusinessException(org.meveo.admin.exception.BusinessException) ArrayList(java.util.ArrayList) Workflow(org.meveo.model.wf.Workflow) WFAction(org.meveo.model.wf.WFAction)

Example 13 with BusinessException

use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.

the class ReportExtractService method evaluateStringExpression.

private String evaluateStringExpression(String expression, ReportExtract re) throws BusinessException, ELException {
    if (!expression.startsWith("#{")) {
        return expression;
    }
    String result = null;
    if (StringUtils.isBlank(expression)) {
        return result;
    }
    Map<Object, Object> userMap = new HashMap<>();
    userMap.put("re", re);
    Object res = MeveoValueExpressionWrapper.evaluateExpression(expression, userMap, String.class);
    try {
        result = (String) res;
    } catch (Exception e) {
        throw new BusinessException("Expression " + expression + " do not evaluate to String but " + res);
    }
    return result;
}
Also used : BusinessException(org.meveo.admin.exception.BusinessException) HashMap(java.util.HashMap) ELException(org.meveo.elresolver.ELException) IOException(java.io.IOException) BusinessException(org.meveo.admin.exception.BusinessException)

Example 14 with BusinessException

use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.

the class GitClient method commit.

/**
 * Stage the files correspondings to the given pattern and create commit
 *
 * @param gitRepository Repository to commit
 * @param patterns      Pattern of the files to stage
 * @param message       Commit message
 * @throws UserNotAuthorizedException if user does not have write access to the repository
 * @throws BusinessException          if repository cannot be open or commited
 * @throws IllegalArgumentException   if pattern list is empty
 */
public void commit(GitRepository gitRepository, List<String> patterns, String message) throws BusinessException {
    MeveoUser user = currentUser.get();
    if (!GitHelper.hasWriteRole(user, gitRepository)) {
        throw new UserNotAuthorizedException(user.getUserName());
    }
    final File repositoryDir = GitHelper.getRepositoryDir(user, gitRepository.getCode());
    keyLock.lock(gitRepository.getCode());
    try (Git git = Git.open(repositoryDir)) {
        final AddCommand add = git.add();
        if (CollectionUtils.isNotEmpty(patterns)) {
            patterns.forEach(add::addFilepattern);
            add.call();
            final Status status = git.status().call();
            final RmCommand rm = git.rm();
            boolean doRm = false;
            for (String missing : status.getMissing()) {
                if (patterns.contains(missing) || patterns.contains(".")) {
                    rm.addFilepattern(missing);
                    doRm = true;
                }
            }
            if (doRm) {
                rm.call();
            }
            if (status.hasUncommittedChanges()) {
                try {
                    RevCommit commit = git.commit().setMessage(message).setAuthor(user.getUserName(), user.getMail()).setAllowEmpty(false).setCommitter(user.getUserName(), user.getMail()).call();
                    Set<String> modifiedFiles = new HashSet<>();
                    modifiedFiles.addAll(status.getAdded());
                    modifiedFiles.addAll(status.getChanged());
                    modifiedFiles.addAll(status.getModified());
                    modifiedFiles.addAll(status.getRemoved());
                    List<DiffEntry> diffs = getDiffs(gitRepository, commit);
                    commitedEvent.fire(new CommitEvent(gitRepository, modifiedFiles, diffs));
                } catch (EmptyCommitException e) {
                // NOOP
                }
            }
        } else {
            throw new IllegalArgumentException("Cannot commit repository " + gitRepository.getCode() + " : no staged files");
        }
    } catch (IOException e) {
        throw new BusinessException("Cannot open repository " + gitRepository.getCode(), e);
    } catch (GitAPIException e) {
        throw new BusinessException("Cannot create commit on repository " + gitRepository.getCode(), e);
    } finally {
        keyLock.unlock(gitRepository.getCode());
    }
}
Also used : Status(org.eclipse.jgit.api.Status) EmptyCommitException(org.eclipse.jgit.api.errors.EmptyCommitException) IOException(java.io.IOException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) BusinessException(org.meveo.admin.exception.BusinessException) Git(org.eclipse.jgit.api.Git) UserNotAuthorizedException(org.meveo.admin.exception.UserNotAuthorizedException) CommitEvent(org.meveo.event.qualifier.git.CommitEvent) MeveoUser(org.meveo.security.MeveoUser) RmCommand(org.eclipse.jgit.api.RmCommand) File(java.io.File) AddCommand(org.eclipse.jgit.api.AddCommand) RevCommit(org.eclipse.jgit.revwalk.RevCommit) HashSet(java.util.HashSet) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 15 with BusinessException

use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.

the class GitClient method createGitMeveoFolder.

protected void createGitMeveoFolder(GitRepository gitRepository, File repoDir) throws BusinessException {
    MeveoUser user = currentUser.get();
    keyLock.lock(gitRepository.getCode());
    try (Git git = Git.init().setDirectory(repoDir).call()) {
        // Init repo with a dummy commit
        new File(repoDir, "README.md").createNewFile();
        git.add().addFilepattern(".").call();
        git.commit().setMessage("First commit").setAuthor(user.getUserName(), user.getMail()).setCommitter(user.getUserName(), user.getMail()).call();
    } catch (GitAPIException | IOException e) {
        repoDir.delete();
        throw new BusinessException("Error initating repository " + gitRepository.getCode(), e);
    } finally {
        keyLock.unlock(gitRepository.getCode());
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) BusinessException(org.meveo.admin.exception.BusinessException) Git(org.eclipse.jgit.api.Git) MeveoUser(org.meveo.security.MeveoUser) IOException(java.io.IOException) File(java.io.File)

Aggregations

BusinessException (org.meveo.admin.exception.BusinessException)229 IOException (java.io.IOException)97 File (java.io.File)59 HashMap (java.util.HashMap)50 EntityDoesNotExistsException (org.meveo.api.exception.EntityDoesNotExistsException)50 ArrayList (java.util.ArrayList)48 MeveoApiException (org.meveo.api.exception.MeveoApiException)39 ELException (org.meveo.elresolver.ELException)39 CustomFieldTemplate (org.meveo.model.crm.CustomFieldTemplate)38 CustomEntityTemplate (org.meveo.model.customEntities.CustomEntityTemplate)37 Map (java.util.Map)34 BundleKey (org.jboss.seam.international.status.builder.BundleKey)30 TransactionAttribute (javax.ejb.TransactionAttribute)28 CustomEntityInstance (org.meveo.model.customEntities.CustomEntityInstance)27 List (java.util.List)25 MeveoModule (org.meveo.model.module.MeveoModule)25 NoResultException (javax.persistence.NoResultException)24 HashSet (java.util.HashSet)22 Response (javax.ws.rs.core.Response)22 Collection (java.util.Collection)20