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