org.mayocat.cms.home.store.jdbi.mapper.HomePageMapper.java Source code

Java tutorial

Introduction

Here is the source code for org.mayocat.cms.home.store.jdbi.mapper.HomePageMapper.java

Source

/*
 * Copyright (c) 2012, Mayocat <hello@mayocat.org>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.mayocat.cms.home.store.jdbi.mapper;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.LocaleUtils;
import org.mayocat.cms.home.model.HomePage;
import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.tweak.ResultSetMapper;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;

/**
 * @version $Id: 951d7f763075f780fd20ca03b7a58f86e4a62f96 $
 */
public class HomePageMapper implements ResultSetMapper<HomePage> {
    @Override
    public HomePage map(int index, ResultSet resultSet, StatementContext ctx) throws SQLException {
        HomePage homePage = new HomePage();
        homePage.setId((UUID) resultSet.getObject("id"));

        if (!Strings.isNullOrEmpty(resultSet.getString("localization_data"))) {
            ObjectMapper mapper = new ObjectMapper();
            try {
                Map<Locale, Map<String, Object>> localizedVersions = Maps.newHashMap();
                Map[] data = mapper.readValue(resultSet.getString("localization_data"), Map[].class);
                for (Map map : data) {
                    localizedVersions.put(LocaleUtils.toLocale((String) map.get("locale")),
                            (Map) map.get("entity"));
                }
                homePage.setLocalizedVersions(localizedVersions);
            } catch (IOException e) {
                throw new SQLException("Failed to de-serialize localization JSON data", e);
            }
        }

        return homePage;
    }
}