Skip to content

Commit

Permalink
Merge branch 'issue/32'
Browse files Browse the repository at this point in the history
  • Loading branch information
n4devca committed May 7, 2019
2 parents d005973 + 7dc2c19 commit df907fc
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,21 @@ public ModelAndView postOne(@PathVariable("clientid") Long pId,
@ModelAttribute("client") ClientView pClientView,
BindingResult pResult) {

boolean mustSave = true;

this.clientViewValidator.validate(pClientView, pResult);
ModelAndView mv = getEditViewAndDependencies(pResult);

if (pAction.startsWith("action_")) {
doAction(pClientView, pAction);
mustSave = doAction(pClientView, pAction);
} else if (pAction.equals("delete")) {
return this.deleteOne(pId);
} else if (!pResult.hasErrors()) {
mv.addObject("client", pClientView);
}

if (!pResult.hasErrors() && mustSave) {
// Save
pClientView = this.clientService.update(pId, pClientView);
mv.addObject("client", pClientView);
}

return mv;
Expand All @@ -158,23 +162,24 @@ public ModelAndView deleteOne(@PathVariable("clientid") Long pId) {
}


private void doAction(ClientView pClientDto, String pAction) {
// action_remove_redirect_url.0
private boolean doAction(ClientView pClientDto, String pAction) {
boolean mustSave = false;
String actionStr = pAction;
Integer idx = null;
if (pAction.contains(".")) {
String[] actionData = pAction.split("\\.");

// TODO(RG) Manage exception properly
if (actionData == null || actionData.length != 2) {
throw new ServerException(ServerExceptionCode.INVALID_PARAMETER);
}
actionStr = actionData[0];
// TODO(RG) Manage exception
idx = Integer.parseInt(actionData[1]);
}

ClientViewAction action = ClientViewAction.from(actionStr);


if (action != null) {

switch (action) {
Expand All @@ -185,16 +190,20 @@ private void doAction(ClientView pClientDto, String pAction) {
pClientDto.getRedirections().add("https://");

case action_remove_contact:
pClientDto.getContacts().remove(idx);
pClientDto.removeContactAt(idx);
mustSave = true;
break;
case action_remove_redirect_url:
pClientDto.getRedirections().remove(idx);
pClientDto.removeRedirectionAt(idx);
mustSave = true;
break;
default:
break;
}

}

return mustSave;
}

private ModelAndView getEditViewAndDependencies(BindingResult pResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,22 @@ public ModelAndView saveUser(@ModelAttribute("dto") UserFormDto pUserFormDto,

final ModelAndView modelAndView = getBasicProfilePage(pResult.getModel());

boolean toSave = true;

// Adding new info
if (hasAction(pUserFormDto)) {

final String action = removeIdFromAction(pUserFormDto.getAction());

switch (action) {
case ACTION_ADD:
toSave = false;
addNewTypeToDto(pUserFormDto);
break;
case ACTION_REMOVE:
removeTypeToDto(pUserFormDto);
break;
}

modelAndView.addObject("dto", pUserFormDto);
return modelAndView;
}

// If we have some error, return
Expand All @@ -148,9 +148,12 @@ public ModelAndView saveUser(@ModelAttribute("dto") UserFormDto pUserFormDto,
}

// Or Save
UserView userView = userService.update(pUser.getId(), pUserFormDto.getUserView());
modelAndView.addObject("dto", new UserFormDto(userView));
if (toSave) {
UserView userView = userService.update(pUser.getId(), pUserFormDto.getUserView());
pUserFormDto = new UserFormDto(userView);
}

modelAndView.addObject("dto", pUserFormDto);
return modelAndView;
}

Expand Down Expand Up @@ -248,6 +251,7 @@ private String removeIdFromAction(String pAction) {
private void addNewTypeToDto(UserFormDto pUserFormDto) {
final String userInfoTypeCode = pUserFormDto.getUserInfoType();
final UserInfoView userInfoTypeView = this.userInfoTypeService.findByCode(userInfoTypeCode);
userInfoTypeView.setRefId(-Utils.nextPositiveId());
pUserFormDto.getUserView().getUserInfos().add(userInfoTypeView);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ public void setContacts(List<String> pContacts) {
contacts = pContacts;
}

/**
* @param pIdx The index of the element to remove
*/
public void removeContactAt(int pIdx) {
if (contacts != null && contacts.size() > pIdx) {
contacts.remove(pIdx);
}
}

/**
* @return the redirections
*/
Expand All @@ -231,6 +240,16 @@ public void setRedirections(List<String> pRedirections) {
redirections = pRedirections;
}

/**
* @param pIdx The index of the element to remove
*/
public void removeRedirectionAt(int pIdx) {
if (redirections != null && redirections.size() > pIdx) {
redirections.remove(pIdx);
}
}


/**
* @return the grants
*/
Expand Down
2 changes: 1 addition & 1 deletion aegaeon-server/src/main/resources/static/css/aegaeon.css
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ a {
}

.input-group-item .form-input {
margin: 0;
margin: 0 0 0.5em 0;
}

.input-group-strech {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ <h1 th:text="#{page.adminclients.title}">Clients</h1>
<input type="text" class="form-input" th:field="*{contacts[__${cidx.index}__]}"/>
</div>
<div class="input-group-item">
<button class="btn-link" name="action" type="submit" th:value="'remove_' + ${cidx.index}">
<button class="btn-link" name="action" type="submit" th:value="${T(ca.n4dev.aegaeon.server.view.ClientViewAction).action_remove_contact} + '.' + ${cidx.index}">
[[#{action.remove}]]
</button>
</div>
Expand Down
3 changes: 3 additions & 0 deletions aegaeon-server/src/main/resources/templates/user/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<h1 th:text="#{page.useraccount.title}">User Profile</h1>
</div>
<div class="page-header-action is-right">

<span th:if="${status}" th:text="#{${'form.status.' + status}"></span>

<button class="btn btn-default" name="submit" type="submit" value="save" th:text="#{action.save}">
Save
</button>
Expand Down

0 comments on commit df907fc

Please sign in to comment.