/*
* #%L
* BroadleafCommerce Open Admin Platform
* %%
* Copyright (C) 2009 - 2015 Broadleaf Commerce
* %%
* 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.
* #L%
*/
package org.broadleafcommerce.openadmin.web.service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.broadleafcommerce.common.media.domain.Media;
import org.broadleafcommerce.common.media.domain.MediaDto;
import org.broadleafcommerce.common.persistence.EntityConfiguration;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.annotation.Resource;
/**
* @author Chad Harchar (charchar)
*/
@Service("blMediaBuilderService")
public class MediaBuilderServiceImpl implements MediaBuilderService {
private static final Log LOG = LogFactory.getLog(MediaBuilderServiceImpl.class);
@Resource(name="blEntityConfiguration")
protected EntityConfiguration entityConfiguration;
@Override
public Media convertJsonToMedia(String json, Class<?> type) {
if (json != null && !"".equals(json)) {
try {
ObjectMapper om = new ObjectMapper();
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return (Media) om.readValue(json, type);
} catch (Exception e) {
LOG.warn("Error parsing json to media " + json, e);
}
}
return entityConfiguration.createEntityInstance(MediaDto.class.getName(), MediaDto.class);
}
}