-
Notifications
You must be signed in to change notification settings - Fork 188
/
AbstractOrgTabPanel.java
217 lines (181 loc) · 8.59 KB
/
AbstractOrgTabPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* Copyright (c) 2010-2017 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.web.page.admin.orgs;
import java.util.ArrayList;
import java.util.List;
import com.evolveum.midpoint.prism.query.ObjectFilter;
import com.evolveum.midpoint.web.component.util.SelectableBean;
import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.markup.html.tabs.AbstractTab;
import org.apache.wicket.extensions.markup.html.tabs.ITab;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import com.evolveum.midpoint.gui.api.component.BasePanel;
import com.evolveum.midpoint.gui.api.model.LoadableModel;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.ObjectQueryUtil;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.component.AjaxTabbedPanel;
import com.evolveum.midpoint.web.component.TabbedPanel;
import com.evolveum.midpoint.web.session.SessionStorage;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType;
public abstract class AbstractOrgTabPanel extends BasePanel {
private static final long serialVersionUID = 1L;
private static final Trace LOGGER = TraceManager.getTrace(AbstractOrgTabPanel.class);
public static final String PARAM_ORG_RETURN = "org";
private static final String DOT_CLASS = OrgTreeAssignablePanel.class.getName() + ".";
private static final String OPERATION_LOAD_ORG_UNIT = DOT_CLASS + "loadOrgUnit";
private static final String OPERATION_LOAD_ASSIGNABLE_ITEMS = DOT_CLASS + "loadAssignableOrgs";
private String ID_TABS = "tabs";
private List<PrismObject<OrgType>> roots;
public AbstractOrgTabPanel(String id, PageBase pageBase) {
super(id);
setParent(pageBase);
initLayout();
}
private void initLayout() {
final IModel<List<ITab>> tabModel = new LoadableModel<List<ITab>>(false) {
private static final long serialVersionUID = 1L;
@Override
protected List<ITab> load() {
LOGGER.debug("Loading org. roots for tabs for tabbed panel.");
roots = loadOrgRoots();
final List<ITab> tabs = new ArrayList<>();
for (PrismObject<OrgType> root : roots) {
final String oid = root.getOid();
tabs.add(new AbstractTab(createTabTitle(root)) {
private static final long serialVersionUID = 1L;
private int tabId = tabs.size();
@Override
public WebMarkupContainer getPanel(String panelId) {
add(new AjaxEventBehavior("load") {
private static final long serialVersionUID = 1L;
protected void onEvent(final AjaxRequestTarget target) {
SessionStorage storage = getPageBase().getSessionStorage();
storage.getUsers().setSelectedTabId(tabId);
}
}
);
Panel panel = createTreePanel(panelId, new Model(oid), getPageBase());
panel.setOutputMarkupId(true);
return panel;
}
});
}
LOGGER.debug("Tab count is {}", new Object[]{tabs.size()});
return tabs;
}
};
final SessionStorage storage = getPageBase().getSessionStorage();
int selectedTab = storage.getUsers().getSelectedTabId() == -1 ? 0 : storage.getUsers().getSelectedTabId();
List<ITab> tabsList = tabModel.getObject();
if (tabsList == null || (selectedTab > tabsList.size() - 1)) {
storage.getUsers().setSelectedTabId(0);
selectedTab = 0;
}
AjaxTabbedPanel<ITab> tabbedPanel = new AjaxTabbedPanel<ITab>(ID_TABS, tabModel.getObject(), new Model<>(selectedTab), null){
private static final long serialVersionUID = 1L;
@Override
public TabbedPanel<ITab> setSelectedTab(int index) {
changeTabPerformed(index);
return super.setSelectedTab(index);
}
};
tabbedPanel.setOutputMarkupId(true);
if (tabsList == null || tabsList.size() == 0){
tabbedPanel.setVisible(false);
}
add(tabbedPanel);
}
protected Panel getPanel(){
if (get(ID_TABS).get("panel") instanceof Panel) {
return (Panel) get(ID_TABS).get("panel");
}
return null;
}
public AjaxTabbedPanel<ITab> getTabbedPanel(){
return (AjaxTabbedPanel) get(ID_TABS);
}
protected abstract Panel createTreePanel(String id, Model<String> model, PageBase pageBase);
private IModel<String> createTabTitle(final PrismObject<OrgType> org) {
return new AbstractReadOnlyModel<String>() {
private static final long serialVersionUID = 1L;
@Override
public String getObject() {
PolyString displayName = org.getPropertyRealValue(OrgType.F_DISPLAY_NAME, PolyString.class);
if (displayName != null) {
return displayName.getOrig();
}
return WebComponentUtil.getName(org);
}
};
}
private List<PrismObject<OrgType>> loadOrgRoots() {
Task task = getPageBase().createSimpleTask(OPERATION_LOAD_ORG_UNIT);
OperationResult result = new OperationResult(OPERATION_LOAD_ORG_UNIT);
List<PrismObject<OrgType>> list = new ArrayList<>();
try {
ObjectQuery query = ObjectQueryUtil.createRootOrgQuery(getPageBase().getPrismContext());
ObjectFilter assignableItemsFilter = getAssignableItemsFilter();
if (assignableItemsFilter != null){
query.addFilter(assignableItemsFilter);
}
list = getPageBase().getModelService().searchObjects(OrgType.class, query, null, task, result);
// Sort org roots by displayOrder, if not set push the org to the end
list.sort((o1, o2) -> (o1.getRealValue().getDisplayOrder() == null ? Integer.MAX_VALUE : o1.getRealValue().getDisplayOrder())
- (o2.getRealValue().getDisplayOrder() == null ? Integer.MAX_VALUE : o2.getRealValue().getDisplayOrder()));
if (list.isEmpty() && isWarnMessageVisible()) {
warn(getString("PageOrgTree.message.noOrgStructDefined"));
}
} catch (Exception ex) {
LoggingUtils.logUnexpectedException(LOGGER, "Unable to load org. unit", ex);
result.recordFatalError("Unable to load org unit", ex);
} finally {
result.computeStatus();
}
if (WebComponentUtil.showResultInPage(result)) {
getPageBase().showResult(result);
}
return list;
}
protected ObjectFilter getAssignableItemsFilter(){
return null;
}
protected boolean isWarnMessageVisible(){
return true;
}
protected void changeTabPerformed(int index){
if (roots != null && index >= 0 && index <= roots.size()){
SessionStorage storage = getPageBase().getSessionStorage();
SelectableBean<OrgType> selected = new SelectableBean<OrgType>();
selected.setValue(roots.get(index).asObjectable());
storage.getUsers().setSelectedItem(selected);
storage.getUsers().setSelectedTabId(index);
}
}
}