001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.runtime.userprofile;
021
022import java.util.Collections;
023import java.util.List;
024
025import com.google.common.collect.Lists;
026
027import org.apache.isis.applib.profiles.Localization;
028import org.apache.isis.core.commons.debug.DebugBuilder;
029import org.apache.isis.core.commons.debug.DebuggableWithTitle;
030import org.apache.isis.core.commons.exceptions.IsisException;
031import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
032
033public class UserProfile implements DebuggableWithTitle {
034
035    public UserProfile() {
036    }
037
038    // ///////////////////////////////////////////////////////
039    // Perspective
040    // ///////////////////////////////////////////////////////
041
042    private PerspectiveEntry entry;
043
044    public PerspectiveEntry getPerspective() {
045        if (entry == null) {
046            if (entries.size() == 0) {
047                throw new IsisException("No perspective in user profile");
048            } else {
049                entry = entries.get(0);
050            }
051        }
052        return entry;
053    }
054
055    public PerspectiveEntry newPerspective(final String name) {
056        entry = new PerspectiveEntry();
057        entry.setName(name);
058        entries.add(entry);
059        return entry;
060    }
061
062    public void removeCurrent() {
063        if (entries.size() > 1) {
064            entries.remove(entry);
065            entry = entries.get(0);
066        }
067    }
068
069    // ///////////////////////////////////////////////////////
070    // Perspective Entries
071    // ///////////////////////////////////////////////////////
072
073    private final List<PerspectiveEntry> entries = Lists.newArrayList();
074
075    public PerspectiveEntry getPerspective(final String name) {
076        for (final PerspectiveEntry entry : entries) {
077            if (entry.getName().equals(name)) {
078                return entry;
079            }
080        }
081        throw new IsisException("No perspective " + name);
082    }
083
084    public void addToPerspectives(final PerspectiveEntry perspective) {
085        final PerspectiveEntry e = new PerspectiveEntry();
086        e.copy(perspective);
087        entries.add(e);
088    }
089
090    public List<String> list() {
091        final List<String> list = Lists.newArrayList();
092        for (final PerspectiveEntry entry : entries) {
093            list.add(entry.getName());
094        }
095        return list;
096    }
097
098    public void select(final String name) {
099        for (final PerspectiveEntry entry : entries) {
100            if (entry.getName().equals(name)) {
101                this.entry = entry;
102                break;
103            }
104        }
105    }
106
107    public void copy(final UserProfile template) {
108        for (final PerspectiveEntry entry : template.entries) {
109            final PerspectiveEntry e = new PerspectiveEntry();
110            e.copy(entry);
111            entries.add(e);
112        }
113        options.copy(template.getOptions());
114    }
115
116    /**
117     * Introduced for debugging.
118     */
119    public List<PerspectiveEntry> getEntries() {
120        return Collections.unmodifiableList(entries);
121    }
122
123    // ///////////////////////////////////////////////////////
124    // Options
125    // ///////////////////////////////////////////////////////
126
127    private final Options options = new Options();
128
129    public Options getOptions() {
130        return options;
131    }
132
133    public void addToOptions(final String name, final String value) {
134        options.addOption(name, value);
135    }
136
137    // ///////////////////////////////
138    // Localization
139    // ///////////////////////////////
140
141    private Localization localization;
142
143    public Localization getLocalization() {
144        return localization;
145    }
146
147    public void setLocalization(final Localization localization) {
148        this.localization = localization;
149    }
150
151    // ///////////////////////////////
152    // Save
153    // ///////////////////////////////
154
155    public void saveObjects(final List<ObjectAdapter> objects) {
156        entry.save(objects);
157    }
158
159    public String debugTitle() {
160        return "User Profle";
161    }
162    
163    public void debugData(DebugBuilder debug) {
164        debug.appendln("Localization", localization);
165        debug.appendln("Options", options);
166        debug.appendln("Entry", entry);
167    }
168}