public Object execute(Method m,
Object[] args,
EntityEnterpriseContext ctx) throws CreateException {
try
{
// Extract pk
Object id = null;
Iterator it = jawsEntity.getPkFields();
if (jawsEntity.hasCompositeKey())
{
try
{
id = jawsEntity.getPrimaryKeyClass().newInstance();
} catch (InstantiationException e)
{
throw new EJBException("Could not create primary key",e);
}
while (it.hasNext())
{
PkFieldMetaData pkField = (PkFieldMetaData)it.next();
Field from = pkField.getCMPField();
Field to = pkField.getPkField();
to.set(id, from.get(ctx.getInstance()));
}
} else
{
PkFieldMetaData pkField = (PkFieldMetaData)it.next();
Field from = pkField.getCMPField();
id = from.get(ctx.getInstance());
}
if (log.isDebugEnabled())
{
log.debug("Create, id is "+id);
}
// Check duplicate
if (beanExistsCommand.execute(id))
{
throw new DuplicateKeyException("Entity with key "+id+" already exists");
}
// Insert in db
try
{
jdbcExecute(ctx);
} catch (Exception e)
{
if (log.isDebugEnabled())
log.debug("Exception", e);
throw new CreateException("Could not create entity:"+e);
}
return id;
} catch (IllegalAccessException e)
{
if (log.isDebugEnabled())
log.debug("IllegalAccessException", e);
throw new CreateException("Could not create entity:"+e);
}
}
|