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 java.io.Serializable;
022
023import org.apache.commons.lang.StringUtils;
024import org.nuxeo.common.xmap.annotation.XNode;
025import org.nuxeo.common.xmap.annotation.XObject;
026
027/**
028 * @since 8.4
029 */
030@XObject("startupPage")
031public class LoginStartupPage implements Serializable, Comparable<LoginStartupPage> {
032
033    private static final long serialVersionUID = 1L;
034
035    @XNode("@priority")
036    protected int priority;
037
038    @XNode("path")
039    protected String path;
040
041    public String getPath() {
042        return path;
043    }
044
045    public int getPriority() {
046        return priority;
047    }
048
049    @Override
050    public boolean equals(Object obj) {
051        if (obj instanceof LoginStartupPage && path != null) {
052            return path.equals(((LoginStartupPage) obj).getPath())
053                    && priority == ((LoginStartupPage) obj).getPriority();
054        }
055        return super.equals(obj);
056    }
057
058    @Override
059    protected LoginStartupPage clone() {
060        LoginStartupPage clone = new LoginStartupPage();
061        clone.path = path;
062        clone.priority = priority;
063        return clone;
064    }
065
066    @Override
067    public String toString() {
068        return path + "," + priority;
069    }
070
071    @Override
072    public int compareTo(LoginStartupPage o) {
073        if (o == null || priority > o.getPriority()) {
074            return 1;
075        }
076        if (priority < o.getPriority()) {
077            return -1;
078        }
079        return 0;
080    }
081
082    public void merge(LoginStartupPage newStartupPage) {
083        if (StringUtils.isNotEmpty(newStartupPage.path)) {
084            path = newStartupPage.path;
085        }
086        // Keep highest priority
087        if (newStartupPage.compareTo(this) > 0) {
088            priority = newStartupPage.priority;
089        }
090    }
091
092}