com.neatresults.mgnltweaks.ui.field.TemplateIdConverter.java Source code

Java tutorial

Introduction

Here is the source code for com.neatresults.mgnltweaks.ui.field.TemplateIdConverter.java

Source

/**
 *
 * Copyright 2015 by Jan Haderka <jan.haderka@neatresults.com>
 *
 * This file is part of neat-tweaks module.
 *
 * Neat-tweaks is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * Neat-tweaks is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with neat-tweaks.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @license GPL-3.0 <http://www.gnu.org/licenses/gpl.txt>
 *
 * Should you require distribution under alternative license in order to
 * use neat-tweaks commercially, please contact owner at the address above.
 *
 */
package com.neatresults.mgnltweaks.ui.field;

import java.util.Locale;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.neatresults.mgnltweaks.NeatUtil;

import info.magnolia.ui.form.field.converter.BaseIdentifierToPathConverter;

/**
 * TemplatePathConverter converts node path to link to freemarker template and back (mostly just adding/removing .ftl extension).
 */
public class TemplateIdConverter extends BaseIdentifierToPathConverter {

    private static final Logger log = LoggerFactory.getLogger(TemplateIdConverter.class);

    private String workspace;

    public void setWorkspace(String workspace) {
        super.setWorkspaceName(workspace);
        this.workspace = workspace;
    }

    @Override
    public String convertToModel(String path, Class<? extends String> targetType, Locale locale)
            throws ConversionException {
        // Null is required for the property to be removed if path is empty
        String res = null;
        if (StringUtils.isBlank(path)) {
            return res;
        }
        String id = StringUtils.substringBefore(StringUtils.removeStart(path, "/modules/"), "/");
        String template = StringUtils.substringAfter(path, "/templates/");
        res = id + ":" + template;
        return res;
    }

    @Override
    public String convertToPresentation(String path, Class<? extends String> targetType, Locale locale)
            throws ConversionException {
        String res = StringUtils.EMPTY;
        if (StringUtils.isBlank(path)) {
            return res;
        }
        return NeatUtil.templateIdToPath(path);
    }
}