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.object.parseable;
021    
022    import org.apache.isis.applib.adapters.Parser;
023    import org.apache.isis.core.commons.authentication.AuthenticationSessionProvider;
024    import org.apache.isis.core.commons.lang.ClassUtil;
025    import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
026    import org.apache.isis.core.metamodel.adapter.map.AdapterMap;
027    import org.apache.isis.core.metamodel.facetapi.FacetAbstract;
028    import org.apache.isis.core.metamodel.facetapi.FacetHolder;
029    import org.apache.isis.core.metamodel.facets.object.parseable.ParseableFacet;
030    import org.apache.isis.core.metamodel.runtimecontext.DependencyInjector;
031    
032    public abstract class ParseableFacetAbstract extends FacetAbstract implements ParseableFacet {
033    
034        private final Class<?> parserClass;
035    
036        // to delegate to
037        private final ParseableFacetUsingParser parseableFacetUsingParser;
038    
039        public ParseableFacetAbstract(final String candidateParserName, final Class<?> candidateParserClass,
040            final FacetHolder holder, final AuthenticationSessionProvider authenticationSessionProvider,
041            final DependencyInjector dependencyInjector, final AdapterMap adapterManager) {
042            super(ParseableFacet.class, holder, false);
043    
044            this.parserClass = ParserUtil.parserOrNull(candidateParserClass, candidateParserName);
045            if (isValid()) {
046                final Parser parser = (Parser) ClassUtil.newInstance(parserClass, FacetHolder.class, holder);
047                this.parseableFacetUsingParser =
048                    new ParseableFacetUsingParser(parser, holder, authenticationSessionProvider, dependencyInjector,
049                        adapterManager);
050            } else {
051                this.parseableFacetUsingParser = null;
052            }
053        }
054    
055        /**
056         * Discover whether either of the candidate parser name or class is valid.
057         */
058        public boolean isValid() {
059            return parserClass != null;
060        }
061    
062        /**
063         * Guaranteed to implement the {@link Parser} class, thanks to generics in the applib.
064         */
065        public Class<?> getParserClass() {
066            return parserClass;
067        }
068    
069        @Override
070        protected String toStringValues() {
071            return parserClass.getName();
072        }
073    
074        @Override
075        public ObjectAdapter parseTextEntry(final ObjectAdapter original, final String entryText) {
076            return parseableFacetUsingParser.parseTextEntry(original, entryText);
077        }
078    
079        @Override
080        public String parseableTitle(final ObjectAdapter existing) {
081            return parseableFacetUsingParser.parseableTitle(existing);
082        }
083    }