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.clobs;
021
022import javax.activation.MimeType;
023import javax.activation.MimeTypeParseException;
024
025import org.apache.isis.applib.adapters.DefaultsProvider;
026import org.apache.isis.applib.adapters.EncoderDecoder;
027import org.apache.isis.applib.adapters.Parser;
028import org.apache.isis.applib.profiles.Localization;
029import org.apache.isis.applib.value.Clob;
030import org.apache.isis.core.commons.config.IsisConfiguration;
031import org.apache.isis.core.metamodel.facetapi.Facet;
032import org.apache.isis.core.metamodel.facetapi.FacetHolder;
033import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderAndFacetAbstract;
034import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext;
035
036public class ClobValueSemanticsProvider extends ValueSemanticsProviderAndFacetAbstract<Clob> implements ClobValueFacet {
037
038    private static final int TYPICAL_LENGTH = 0;
039
040    private static Class<? extends Facet> type() {
041        return ClobValueFacet.class;
042    }
043
044    private static final Clob DEFAULT_VALUE = null;
045
046    /**
047     * Required because implementation of {@link Parser} and
048     * {@link EncoderDecoder}.
049     */
050    public ClobValueSemanticsProvider() {
051        this(null, null, null);
052    }
053
054    public ClobValueSemanticsProvider(final FacetHolder holder, final IsisConfiguration configuration, final ValueSemanticsProviderContext context) {
055        super(type(), holder, Clob.class, TYPICAL_LENGTH, Immutability.IMMUTABLE, EqualByContent.NOT_HONOURED, DEFAULT_VALUE, configuration, context);
056    }
057
058    @Override
059    public String titleString(final Object object, final Localization localization) {
060        return object != null? ((Clob)object).getName(): "[null]";
061    }
062
063    @Override
064    public String titleStringWithMask(final Object value, final String usingMask) {
065        return titleString(value, null);
066    }
067
068
069    // //////////////////////////////////////////////////////////////////
070    // Parser
071    // //////////////////////////////////////////////////////////////////
072
073    @Override
074    public Parser<Clob> getParser() {
075        return null;
076    }
077
078    // //////////////////////////////////////////////////////////////////
079    // DefaultsProvider
080    // //////////////////////////////////////////////////////////////////
081    
082    @Override
083    public DefaultsProvider<Clob> getDefaultsProvider() {
084        return null;
085    }
086    
087    // //////////////////////////////////////////////////////////////////
088    // EncoderDecoder
089    // //////////////////////////////////////////////////////////////////
090
091    @Override
092    protected String doEncode(final Object object) {
093        Clob clob = (Clob)object;
094        return clob.getName() + ":" + clob.getMimeType().getBaseType() + ":" + clob.getChars();
095    }
096
097    @Override
098    protected Clob doRestore(final String data) {
099        final int colonIdx = data.indexOf(':');
100        final String name  = data.substring(0, colonIdx);
101        final int colon2Idx  = data.indexOf(":", colonIdx+1);
102        final String mimeTypeBase = data.substring(colonIdx+1, colon2Idx);
103        final CharSequence chars = data.substring(colon2Idx+1);
104        try {
105            return new Clob(name, new MimeType(mimeTypeBase), chars);
106        } catch (MimeTypeParseException e) {
107            throw new RuntimeException(e);
108        }
109    }
110
111    
112    // /////// toString ///////
113
114    @Override
115    public String toString() {
116        return "ClobValueSemanticsProvider";
117    }
118
119}