/*
 * Copyright (c) 2010-2013 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.users;

import com.evolveum.midpoint.common.security.AuthorizationConstants;
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.application.PageDescriptor;
import com.evolveum.midpoint.web.component.TabbedPanel;
import com.evolveum.midpoint.web.component.util.LoadableModel;
import com.evolveum.midpoint.web.page.admin.users.component.TreeTablePanel;
import com.evolveum.midpoint.web.util.WebMiscUtil;
import com.evolveum.midpoint.xml.ns._public.common.common_2a.OrgType;
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.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lazyman
 */
@PageDescriptor(url = "/admin/org/tree", action = {
        PageAdminUsers.AUTHORIZATION_ORG_ALL,
        AuthorizationConstants.NS_AUTHORIZATION + "#orgTree"})
public class PageOrgTree extends PageAdminUsers {

    private static final Trace LOGGER = TraceManager.getTrace(PageOrgTree.class);

    /**
     * todo what is this for? [lazyman]
     */
    public static final String PARAM_ORG_RETURN = "org";

    private static final String DOT_CLASS = PageOrgTree.class.getName() + ".";
    private static final String OPERATION_LOAD_ORG_UNIT = DOT_CLASS + "loadOrgUnit";

    private static final String ID_TABS = "tabs";


    public PageOrgTree() {
        initLayout();
    }

    private void initLayout() {
        IModel<List<ITab>> tabModel = new LoadableModel<List<ITab>>(false) {

            @Override
            protected List<ITab> load() {
                LOGGER.debug("Loading org. roots for tabs for tabbed panel.");
                List<PrismObject<OrgType>> roots = loadOrgRoots();

                List<ITab> tabs = new ArrayList<ITab>();
                for (PrismObject<OrgType> root : roots) {
                    final String oid = root.getOid();
                    tabs.add(new AbstractTab(createTabTitle(root)) {

                        @Override
                        public WebMarkupContainer getPanel(String panelId) {
                            return new TreeTablePanel(panelId, new Model(oid));
                        }
                    });
                }

                LOGGER.debug("Tab count is {}", new Object[]{tabs.size()});

                return tabs;
            }
        };

        TabbedPanel tabbedPanel = new TabbedPanel(ID_TABS, tabModel, new Model<Integer>(0));
        tabbedPanel.setOutputMarkupId(true);
        add(tabbedPanel);
    }

    private IModel<String> createTabTitle(final PrismObject<OrgType> org) {
        return new AbstractReadOnlyModel<String>() {

            @Override
            public String getObject() {
                PolyString displayName = org.getPropertyRealValue(OrgType.F_DISPLAY_NAME, PolyString.class);
                if (displayName != null) {
                    return displayName.getOrig();
                }

                return WebMiscUtil.getName(org);
            }
        };
    }

    private List<PrismObject<OrgType>> loadOrgRoots() {
        Task task = createSimpleTask(OPERATION_LOAD_ORG_UNIT);
        OperationResult result = new OperationResult(OPERATION_LOAD_ORG_UNIT);

        List<PrismObject<OrgType>> list = null;
        try {
            ObjectQuery query = ObjectQueryUtil.createRootOrgQuery(getPrismContext());
            list = getModelService().searchObjects(OrgType.class, query, null, task, result);

            if (list.isEmpty()) {
                warn(getString("PageOrgTree.message.noOrgStructDefined"));
            }
        } catch (Exception ex) {
            LoggingUtils.logException(LOGGER, "Unable to load org. unit", ex);
            result.recordFatalError("Unable to load org unit", ex);
        } finally {
            result.computeStatus();
        }

        if (WebMiscUtil.showResultInPage(result)) {
            showResult(result);
        }

        return list;
    }
}

@@ -1376,24 +1376,96 @@
 dableModel;%0A
+import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;%0A
 import com.e
@@ -1649,16 +1649,67 @@
 rgType;%0A
+import org.apache.wicket.RestartResponseException;%0A
 import o
@@ -2815,16 +2815,22 @@
         
+final 
 IModel%3CL
@@ -3674,24 +3674,246 @@
 .size()%7D);%0A%0A
+                if (tabs.isEmpty()) %7B%0A                    getSession().warn(getString(%22PageOrgTree.message.noOrgStructDefined%22));%0A                    throw new RestartResponseException(PageUsers.class);%0A                %7D%0A%0A
             
@@ -4038,15 +4038,8 @@
 del%3C
-Integer
 %3E(0)

fix for empty org. structure MID-1739