package org.b3log.latke.repository.jpa;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
import javax.persistence.Id;
import javax.persistence.LockModeType;
import javax.persistence.Query;
import org.b3log.latke.Keys;
import org.b3log.latke.model.User;
import org.b3log.latke.repository.Repositories;
import org.b3log.latke.repository.Repository;
import org.b3log.latke.repository.Transaction;
import org.json.JSONObject;
private static final Logger LOGGER = Logger.getLogger(EntityManagerImpl.class.getName());
@Override
public void persist(
final Object entity) {
final Class<? extends Object> clazz = entity.getClass();
final MetaEntity metaEntity = Entities.getMetaEntity(clazz);
if (null == metaEntity) {
throw new IllegalArgumentException("The specified object[class=" + clazz + ",toString="
+ entity.toString() + "] is not an entity");
}
final String repositoryName = metaEntity.getRepositoryName();
final Repository repository = Repositories.getRepository(repositoryName);
if (null == repository) {
throw new IllegalArgumentException("The specified object[class=" + clazz + ",toString="
+ entity.toString() + "] is not an entity");
}
try {
final JSONObject jsonObject = toJSONObject(entity);
repository.add(jsonObject);
} catch (final Exception e) {
final String errMsg = "Can not persist entity[class=" + clazz + ",toString=" + entity.toString() + "]";
LOGGER.log(Level.SEVERE, errMsg, e);
throw new IllegalArgumentException(errMsg);
}
}
private JSONObject
toJSONObject(
final Object entity)
throws Exception {
final JSONObject ret = new JSONObject();
final Class<? extends Object> clazz = entity.getClass();
final MetaEntity metaEntity = Entities.getMetaEntity(clazz);
final Map<String, Field> fields = metaEntity.getFields();
for (final Map.Entry<String, Field> entityField : fields.entrySet()) {
final String fieldName = entityField.getKey();
final Field field = entityField.getValue();
final Class<?> fieldType = field.getType();
field.setAccessible(true);
final Object fieldValue = field.get(entity);
if (field.isAnnotationPresent(Id.class)) {
if (null != fieldValue) {
ret.put(Keys.OBJECT_ID, fieldValue);
}
continue;
}
if (fieldType.isPrimitive() || String.class.equals(fieldType)) {
ret.put(fieldName, fieldValue);
}
}
return ret;
}
@Override
public <T> T
merge(
final T entity) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void remove(
final Object entity) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public <T> T
find(
final Class<T> entityClass,
final Object primaryKey) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
final Object primaryKey) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void lock(
final Object entity,
final LockModeType lockMode) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void refresh(
final Object entity) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public boolean contains(
final Object entity) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
final Class resultClass) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
final String resultSetMapping) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
final Repository userRepository = Repositories.getRepository(User.USER);
final Transaction transaction = userRepository.beginTransaction();
return new EntityTransactionImpl(transaction);
}
}