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.progmodel.facets.value.password;
021
022import org.apache.isis.applib.adapters.EncoderDecoder;
023import org.apache.isis.applib.adapters.Parser;
024import org.apache.isis.applib.profiles.Localization;
025import org.apache.isis.applib.value.Password;
026import org.apache.isis.core.commons.config.IsisConfiguration;
027import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
028import org.apache.isis.core.metamodel.facetapi.Facet;
029import org.apache.isis.core.metamodel.facetapi.FacetHolder;
030import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderAndFacetAbstract;
031import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
032
033public class PasswordValueSemanticsProvider extends ValueSemanticsProviderAndFacetAbstract<Password> implements
034    PasswordValueFacet {
035
036    public static Class<? extends Facet> type() {
037        return PasswordValueFacet.class;
038    }
039
040    private static final Password DEFAULT_VALUE = null; // no default
041    private static final int TYPICAL_LENGTH = 12;
042
043    /**
044     * Required because implementation of {@link Parser} and {@link EncoderDecoder}.
045     */
046    public PasswordValueSemanticsProvider() {
047        this(null, null, null);
048    }
049
050    public PasswordValueSemanticsProvider(final FacetHolder holder, final IsisConfiguration configuration,
051        final ValueSemanticsProviderContext context) {
052        super(type(), holder, Password.class, TYPICAL_LENGTH, Immutability.IMMUTABLE, EqualByContent.HONOURED, DEFAULT_VALUE,
053            configuration, context);
054    }
055
056    // //////////////////////////////////////////////////////////////////
057    // Parser
058    // //////////////////////////////////////////////////////////////////
059
060    @Override
061    protected Password doParse(final Object context, final String text) {
062        return new Password(text);
063    }
064
065    @Override
066    public String titleString(final Object object, final Localization localization) {
067        return object == null ? "" : password(object).toString();
068    }
069
070    @Override
071    public String titleStringWithMask(final Object object, final String usingMask) {
072        return titleString(object, null);
073    }
074
075    // //////////////////////////////////////////////////////////////////
076    // EncoderDecoder
077    // //////////////////////////////////////////////////////////////////
078
079    @Override
080    protected String doEncode(final Object object) {
081        return password(object).getPassword();
082    }
083
084    @Override
085    protected Password doRestore(final String data) {
086        return new Password(data);
087    }
088
089    // //////////////////////////////////////////////////////////////////
090    // PasswordValueFacet
091    // //////////////////////////////////////////////////////////////////
092
093    @Override
094    public boolean checkPassword(final ObjectAdapter object, final String password) {
095        return password(object.getObject()).checkPassword(password);
096    }
097
098    @Override
099    public String getEditText(final ObjectAdapter object) {
100        return object == null ? "" : password(object).getPassword();
101    }
102
103    @Override
104    public ObjectAdapter createValue(final String password) {
105        return getAdapterManager().adapterFor(new Password(password));
106    }
107
108    private Password password(final Object object) {
109        if (object instanceof ObjectAdapter) {
110            return (Password) ((ObjectAdapter) object).getObject();
111        } else {
112            return (Password) object;
113        }
114    }
115
116    // /////// toString ///////
117
118    @Override
119    public String toString() {
120        return "PasswordValueSemanticsProvider";
121    }
122
123}