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.object.regex;
021
022import org.apache.isis.applib.events.ValidityEvent;
023import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
024import org.apache.isis.core.metamodel.facetapi.Facet;
025import org.apache.isis.core.metamodel.facetapi.FacetHolder;
026import org.apache.isis.core.metamodel.facets.MultipleValueFacetAbstract;
027import org.apache.isis.core.metamodel.interactions.ProposedHolder;
028import org.apache.isis.core.metamodel.interactions.ValidityContext;
029
030public abstract class RegExFacetAbstract extends MultipleValueFacetAbstract implements RegExFacet {
031
032    public static Class<? extends Facet> type() {
033        return RegExFacet.class;
034    }
035
036    private final String validation;
037    private final String format;
038    private final boolean caseSensitive;
039
040    public RegExFacetAbstract(final String validation, final String format, final boolean caseSensitive, final FacetHolder holder) {
041        super(type(), holder);
042        this.validation = validation;
043        this.format = format;
044        this.caseSensitive = caseSensitive;
045    }
046
047    @Override
048    public String validation() {
049        return validation;
050    }
051
052    @Override
053    public String format() {
054        return format;
055    }
056
057    @Override
058    public boolean caseSensitive() {
059        return caseSensitive;
060    }
061
062    // //////////////////////////////////////////////////////////
063
064    @Override
065    public String invalidates(final ValidityContext<? extends ValidityEvent> context) {
066        if (!(context instanceof ProposedHolder)) {
067            return null;
068        }
069        final ProposedHolder proposedHolder = (ProposedHolder) context;
070        final ObjectAdapter proposedArgument = proposedHolder.getProposed();
071        if (proposedArgument == null) {
072            return null;
073        }
074        final String titleString = proposedArgument.titleString();
075        if (!doesNotMatch(titleString)) {
076            return null;
077        }
078        return "Doesn't match pattern";
079    }
080
081}