cz.muni.fi.editor.webapp.formatters.UserPermissionFormatter.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.fi.editor.webapp.formatters.UserPermissionFormatter.java

Source

/*
*Copyright  2016 Dominik Szalai (emptulik@gmail.com)
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/
/*
 * Copyright  2016 Dominik Szalai (emptulik@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.editor.webapp.formatters;

import cz.muni.fi.editor.webapp.forms.UserPermissionForm;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.Formatter;

import java.text.ParseException;
import java.util.Locale;

/**
 * @author Dominik Szalai - emptulik at gmail.com
 */
@Log4j2
public class UserPermissionFormatter implements Formatter<UserPermissionForm> {
    @Override
    public String print(UserPermissionForm object, Locale locale) {

        if (object != null && object.getId() != null) {
            return object.getId().toString();
        } else {
            return null;
        }
    }

    @Override
    public UserPermissionForm parse(String text, Locale locale) throws ParseException {
        if (StringUtils.isEmpty(text)) {
            throw new ParseException("Empty text.", 0);
        }

        UserPermissionForm upf = new UserPermissionForm();
        upf.setId(Long.valueOf(text));

        return upf;
    }

}