001/*
002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Antoine Taillefer <ataillefer@nuxeo.com>
018 */
019package org.nuxeo.ecm.platform.ui.web.auth.service;
020
021import org.apache.commons.lang3.StringUtils;
022import org.nuxeo.common.xmap.annotation.XNode;
023import org.nuxeo.common.xmap.annotation.XObject;
024
025/**
026 * @since 8.4
027 */
028@XObject("startupPage")
029public class LoginStartupPage implements Comparable<LoginStartupPage> {
030
031    @XNode("@priority")
032    protected int priority;
033
034    @XNode("path")
035    protected String path;
036
037    public String getPath() {
038        return path;
039    }
040
041    public int getPriority() {
042        return priority;
043    }
044
045    @Override
046    public boolean equals(Object obj) {
047        if (obj instanceof LoginStartupPage && path != null) {
048            return path.equals(((LoginStartupPage) obj).getPath())
049                    && priority == ((LoginStartupPage) obj).getPriority();
050        }
051        return super.equals(obj);
052    }
053
054    @Override
055    protected LoginStartupPage clone() {
056        LoginStartupPage clone = new LoginStartupPage();
057        clone.path = path;
058        clone.priority = priority;
059        return clone;
060    }
061
062    @Override
063    public String toString() {
064        return path + "," + priority;
065    }
066
067    @Override
068    public int compareTo(LoginStartupPage o) {
069        if (o == null || priority > o.getPriority()) {
070            return 1;
071        }
072        if (priority < o.getPriority()) {
073            return -1;
074        }
075        return 0;
076    }
077
078    public void merge(LoginStartupPage newStartupPage) {
079        if (StringUtils.isNotEmpty(newStartupPage.path)) {
080            path = newStartupPage.path;
081        }
082        // Keep highest priority
083        if (newStartupPage.compareTo(this) > 0) {
084            priority = newStartupPage.priority;
085        }
086    }
087
088}