Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix for MID-3225 (Tried to retrieve a localized string for a componen…
…t that has not yet been added to the page), dashboard search panel cleanup
- Loading branch information
Showing
2 changed files
with
132 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
228 changes: 115 additions & 113 deletions
228
...gui/src/main/java/com/evolveum/midpoint/web/page/self/component/DashboardSearchPanel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,167 +1,169 @@ | ||
package com.evolveum.midpoint.web.page.self.component; | ||
|
||
import com.evolveum.midpoint.gui.api.component.BasePanel; | ||
import com.evolveum.midpoint.gui.api.util.WebComponentUtil; | ||
import com.evolveum.midpoint.security.api.AuthorizationConstants; | ||
import com.evolveum.midpoint.web.page.admin.resources.PageResources; | ||
import com.evolveum.midpoint.web.page.admin.server.PageTasks; | ||
import com.evolveum.midpoint.web.page.admin.users.PageUsers; | ||
import com.evolveum.midpoint.web.page.admin.users.dto.UsersDto; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.poi.ss.formula.functions.T; | ||
import org.apache.wicket.ajax.AjaxRequestTarget; | ||
import org.apache.wicket.ajax.markup.html.AjaxLink; | ||
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink; | ||
import org.apache.wicket.markup.ComponentTag; | ||
import org.apache.wicket.markup.html.basic.Label; | ||
import org.apache.wicket.markup.html.form.Form; | ||
import org.apache.wicket.markup.html.form.TextField; | ||
import org.apache.wicket.markup.html.list.ListItem; | ||
import org.apache.wicket.markup.html.list.ListView; | ||
import org.apache.wicket.model.IModel; | ||
import org.apache.wicket.model.Model; | ||
import org.apache.wicket.model.util.ListModel; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import com.evolveum.midpoint.gui.api.component.BasePanel; | ||
import com.evolveum.midpoint.gui.api.util.WebComponentUtil; | ||
import com.evolveum.midpoint.security.api.AuthorizationConstants; | ||
import com.evolveum.midpoint.util.logging.Trace; | ||
import com.evolveum.midpoint.util.logging.TraceManager; | ||
import com.evolveum.midpoint.web.component.util.VisibleBehaviour; | ||
import com.evolveum.midpoint.web.page.admin.resources.PageResources; | ||
import com.evolveum.midpoint.web.page.admin.server.PageTasks; | ||
import com.evolveum.midpoint.web.page.admin.users.PageUsers; | ||
|
||
/** | ||
* Created by honchar. | ||
*/ | ||
public class DashboardSearchPanel extends BasePanel<T> { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private static final Trace LOGGER = TraceManager.getTrace(DashboardSearchPanel.class); | ||
|
||
private final String ID_SEARCH_INPUT = "searchInput"; | ||
private final String ID_SEARCH_BUTTON = "searchButton"; | ||
private final String ID_SEARCH_TYPE_ITEM = "searchTypeItem"; | ||
private final String ID_SEARCH_TYPES = "searchTypes"; | ||
private final String ID_BUTTON_LABEL = "buttonLabel"; | ||
private final String ID_SEARCH_FORM = "searchForm"; | ||
private List<String> SEARCH_TYPES; | ||
private final int USER_INDEX = 0; | ||
private final int RESOURCE_INDEX = 1; | ||
private final int TASK_INDEX = 2; | ||
private int selectedSearchType = 0; | ||
|
||
private static Map<SearchType, IModel<String>> SEARCH_TYPES = new HashMap<>(); | ||
|
||
private SearchType selectedSearchType = SearchType.USERS; | ||
|
||
private enum SearchType { | ||
USERS, RESOURCES, TASKS; | ||
} | ||
|
||
public DashboardSearchPanel(String id) { | ||
super(id); | ||
} | ||
|
||
@Override | ||
protected void onInitialize(){ | ||
super.onInitialize(); | ||
SEARCH_TYPES = Arrays.asList( | ||
createStringResource("PageDashboard.search.users").getString(), | ||
createStringResource("PageDashboard.search.resources").getString(), | ||
createStringResource("PageDashboard.search.tasks").getString() | ||
); | ||
|
||
initLayout(); | ||
setOutputMarkupId(true); | ||
} | ||
|
||
protected void initLayout() { | ||
final Form searchForm = new com.evolveum.midpoint.web.component.form.Form(ID_SEARCH_FORM); | ||
final Form<?> searchForm = new com.evolveum.midpoint.web.component.form.Form<>(ID_SEARCH_FORM); | ||
add(searchForm); | ||
searchForm.setOutputMarkupId(true); | ||
|
||
final List<String> accessibleSearchTypes = new ArrayList<>(); | ||
if (WebComponentUtil.isAuthorized(AuthorizationConstants.AUTZ_UI_USERS_ALL_URL, | ||
AuthorizationConstants.AUTZ_UI_USERS_URL)) { | ||
accessibleSearchTypes.add(SEARCH_TYPES.get(USER_INDEX)); | ||
SEARCH_TYPES.put(SearchType.USERS, createStringResource("PageDashboard.search.users")); | ||
} | ||
if (WebComponentUtil.isAuthorized(AuthorizationConstants.AUTZ_UI_RESOURCES_ALL_URL, | ||
AuthorizationConstants.AUTZ_UI_RESOURCES_URL)) { | ||
accessibleSearchTypes.add(SEARCH_TYPES.get(RESOURCE_INDEX)); | ||
SEARCH_TYPES.put(SearchType.RESOURCES, createStringResource("PageDashboard.search.resources")); | ||
} | ||
if (WebComponentUtil.isAuthorized(AuthorizationConstants.AUTZ_UI_TASKS_ALL_URL, | ||
AuthorizationConstants.AUTZ_UI_TASKS_URL)) { | ||
accessibleSearchTypes.add(SEARCH_TYPES.get(TASK_INDEX)); | ||
} | ||
if (accessibleSearchTypes.size() == 0) { | ||
searchForm.setVisible(false); | ||
} else { | ||
final TextField searchInput = new TextField(ID_SEARCH_INPUT, Model.of("")) { | ||
@Override | ||
protected void onComponentTag(final ComponentTag tag) { | ||
super.onComponentTag(tag); | ||
tag.put("placeholder", createStringResource("PageDashboard.search.input").getString()); | ||
} | ||
}; | ||
searchForm.add(searchInput); | ||
final Label buttonLabel = new Label(ID_BUTTON_LABEL, new Model<String>() { | ||
public String getObject() { | ||
return accessibleSearchTypes.get(selectedSearchType); | ||
} | ||
}); | ||
buttonLabel.setOutputMarkupId(true); | ||
|
||
final AjaxSubmitLink searchButton = new AjaxSubmitLink(ID_SEARCH_BUTTON) { | ||
@Override | ||
protected void onSubmit(AjaxRequestTarget target, Form<?> form) { | ||
String searchType = buttonLabel.getDefaultModel().getObject().toString(); | ||
String searchText = searchInput.getValue(); | ||
performSearch(searchType, searchText == null ? "" : searchText); | ||
} | ||
}; | ||
searchButton.setOutputMarkupId(true); | ||
searchButton.add(buttonLabel); | ||
searchForm.add(searchButton); | ||
searchForm.setDefaultButton(searchButton); | ||
|
||
ListView<String> li = new ListView<String>(ID_SEARCH_TYPES, new IModel<List<String>>() { | ||
@Override | ||
public void detach() { | ||
} | ||
|
||
@Override | ||
public List<String> getObject() { | ||
return accessibleSearchTypes; | ||
} | ||
|
||
@Override | ||
public void setObject(List<String> list) { | ||
} | ||
}) { | ||
|
||
@Override | ||
protected void populateItem(final ListItem<String> item) { | ||
final AjaxLink searchTypeLink = new AjaxLink(ID_SEARCH_TYPE_ITEM) { | ||
@Override | ||
public IModel<?> getBody() { | ||
return new Model<>(item.getModel().getObject()); | ||
} | ||
|
||
@Override | ||
public void onClick(AjaxRequestTarget target) { | ||
selectedSearchType = accessibleSearchTypes.indexOf(item.getModelObject()); | ||
target.add(DashboardSearchPanel.this.get(createComponentPath(ID_SEARCH_FORM, ID_SEARCH_BUTTON))); | ||
} | ||
|
||
@Override | ||
protected void onComponentTag(final ComponentTag tag) { | ||
super.onComponentTag(tag); | ||
tag.put("value", item.getModelObject()); | ||
} | ||
|
||
}; | ||
searchTypeLink.setOutputMarkupId(true); | ||
item.add(searchTypeLink); | ||
} | ||
}; | ||
li.setOutputMarkupId(true); | ||
searchForm.add(li); | ||
|
||
|
||
SEARCH_TYPES.put(SearchType.TASKS, createStringResource("PageDashboard.search.tasks")); | ||
} | ||
|
||
TextField<String> searchInput = new TextField<>(ID_SEARCH_INPUT, Model.of("")); | ||
searchInput.add(new VisibleBehaviour(() -> !SEARCH_TYPES.isEmpty())); | ||
searchInput.setOutputMarkupId(true); | ||
searchInput.setOutputMarkupPlaceholderTag(true); | ||
searchForm.add(searchInput); | ||
|
||
final AjaxSubmitLink searchButton = new AjaxSubmitLink(ID_SEARCH_BUTTON) { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
public IModel<?> getBody() { | ||
return SEARCH_TYPES.get(selectedSearchType); | ||
}; | ||
|
||
@Override | ||
protected void onSubmit(AjaxRequestTarget target, Form<?> form) { | ||
performSearch(getSearchText()); | ||
} | ||
}; | ||
searchButton.setOutputMarkupId(true); | ||
searchButton.setOutputMarkupPlaceholderTag(true); | ||
searchForm.add(searchButton); | ||
searchForm.setDefaultButton(searchButton); | ||
|
||
ListView<SearchType> li = new ListView<SearchType>(ID_SEARCH_TYPES, | ||
new ListModel<SearchType>(new ArrayList<>(SEARCH_TYPES.keySet()))) { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
@Override | ||
protected void populateItem(final ListItem<SearchType> item) { | ||
final AjaxLink<String> searchTypeLink = new AjaxLink<String>(ID_SEARCH_TYPE_ITEM) { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
@Override | ||
public IModel<String> getBody() { | ||
return SEARCH_TYPES.get(item.getModelObject()); | ||
} | ||
|
||
@Override | ||
public void onClick(AjaxRequestTarget target) { | ||
selectedSearchType = item.getModelObject(); | ||
target.add(DashboardSearchPanel.this.get(createComponentPath(ID_SEARCH_FORM, ID_SEARCH_BUTTON))); | ||
} | ||
|
||
|
||
}; | ||
searchTypeLink.setOutputMarkupId(true); | ||
item.add(searchTypeLink); | ||
} | ||
}; | ||
li.setOutputMarkupId(true); | ||
searchForm.add(li); | ||
|
||
|
||
} | ||
|
||
private void performSearch(String searchType, String text) { | ||
if (SEARCH_TYPES.indexOf(searchType) == USER_INDEX) { | ||
setResponsePage(new PageUsers(text)); | ||
} else if (SEARCH_TYPES.indexOf(searchType) == RESOURCE_INDEX) { | ||
setResponsePage(new PageResources(text)); | ||
} else if (SEARCH_TYPES.indexOf(searchType) == TASK_INDEX) { | ||
setResponsePage(new PageTasks(text)); | ||
} | ||
|
||
private String getSearchText() { | ||
TextField<String> searchInput = (TextField<String>) get(createComponentPath(ID_SEARCH_FORM, ID_SEARCH_INPUT)); | ||
if (searchInput == null) { | ||
LOGGER.error("cannot find search input component"); | ||
return null; | ||
} | ||
|
||
return searchInput.getModelObject(); | ||
} | ||
|
||
|
||
private void performSearch(String text) { | ||
|
||
switch (selectedSearchType) { | ||
case USERS: | ||
setResponsePage(new PageUsers(text)); | ||
break; | ||
case RESOURCES: | ||
setResponsePage(new PageResources(text)); | ||
break; | ||
case TASKS: | ||
setResponsePage(new PageTasks(text)); | ||
break; | ||
default: | ||
setResponsePage(new PageUsers(text)); | ||
} | ||
|
||
} | ||
|
||
} |