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());
}
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;
}
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;
}
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());
}
}
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());
}
}
Aggregations