Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@
</where>
</select>

<insert id="saveAll">
insert into host (hostname, ipv4, ipv6, os, arch, available_processors, free_memory_size, total_memory_size, free_disk, total_disk, state, cluster_id)
<insert id="saveAll" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
insert into host (hostname, ipv4, ipv6, os, arch, available_processors, free_memory_size, total_memory_size, free_disk, total_disk, state, cluster_id, create_by, update_by, create_time, update_time)
values
<foreach collection='hosts' item='host' separator=','>
(#{host.hostname}, #{host.ipv4}, #{host.ipv6}, #{host.os}, #{host.arch}, #{host.availableProcessors}, #{host.freeMemorySize}, #{host.totalMemorySize}, #{host.freeDisk}, #{host.totalDisk}, #{host.state}, #{host.clusterId})
(#{host.hostname}, #{host.ipv4}, #{host.ipv6}, #{host.os}, #{host.arch}, #{host.availableProcessors}, #{host.freeMemorySize}, #{host.totalMemorySize}, #{host.freeDisk}, #{host.totalDisk}, #{host.state}, #{host.clusterId} ,#{host.createBy},#{host.updateBy},#{host.createTime},#{host.updateTime})
</foreach>
</insert>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
limit 1
</select>

<insert id="saveAll">
insert into repo (base_url, os, arch, repo_id, repo_name, cluster_id)
<insert id="saveAll" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
insert into repo (base_url, os, arch, repo_id, repo_name, cluster_id, create_by, update_by, create_time, update_time)
values
<foreach collection='clusters' item='cluster' separator=','>
(#{cluster.baseUrl},#{cluster.os},#{cluster.arch},#{cluster.repoId},#{cluster.repoName},#{cluster.clusterId})
(#{cluster.baseUrl},#{cluster.os},#{cluster.arch},#{cluster.repoId},#{cluster.repoName},#{cluster.clusterId},#{cluster.createBy},#{cluster.updateBy},#{cluster.createTime},#{cluster.updateTime})
</foreach>
</insert>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
import org.apache.bigtop.manager.dao.annotations.CreateTime;
import org.apache.bigtop.manager.dao.annotations.UpdateBy;
import org.apache.bigtop.manager.dao.annotations.UpdateTime;
import org.apache.bigtop.manager.dao.po.BasePO;
import org.apache.bigtop.manager.server.holder.SessionUserHolder;

import org.apache.commons.lang3.tuple.Pair;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
Expand All @@ -41,6 +42,8 @@

import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Slf4j
Expand All @@ -53,6 +56,7 @@
})
public class AuditingInterceptor implements Interceptor {

@SuppressWarnings("unchecked")
@Override
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
Expand All @@ -62,39 +66,60 @@ public Object intercept(Invocation invocation) throws Throwable {
Object parameter = invocation.getArgs()[1];
log.debug("sqlCommandType {}", sqlCommandType);

if (!(parameter instanceof BasePO)) {
return invocation.proceed();
Collection<Object> objects;
if (parameter instanceof MapperMethod.ParamMap) {
MapperMethod.ParamMap<Object> paramMap = ((MapperMethod.ParamMap<Object>) parameter);
if (paramMap.get("param1") instanceof Collection) {
objects = ((Collection<Object>) paramMap.get("param1"));
} else {
objects = Collections.singletonList(paramMap.get("param1"));
}
} else {
objects = Collections.singletonList(parameter);
}

for (Object o : objects) {
setAuditFields(o, sqlCommandType);
}

return invocation.proceed();
}

private Pair<Long, Timestamp> getAuditInfo() {
// Get the current time and operator
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Long currentUser = SessionUserHolder.getUserId();
log.debug("timestamp: {} currentUser: {}", timestamp, currentUser);
return Pair.of(currentUser, timestamp);
}

private void setAuditFields(Object object, SqlCommandType sqlCommandType) throws IllegalAccessException {

Pair<Long, Timestamp> auditInfo = getAuditInfo();
Long currentUser = auditInfo.getLeft();
Timestamp timestamp = auditInfo.getRight();

// Modify audit fields
List<Field> fields = ClassUtils.getFields(parameter.getClass());
List<Field> fields = ClassUtils.getFields(object.getClass());
if (SqlCommandType.INSERT == sqlCommandType || SqlCommandType.UPDATE == sqlCommandType) {
for (Field field : fields) {
boolean accessible = field.canAccess(parameter);
boolean accessible = field.canAccess(object);
field.setAccessible(true);
if (field.isAnnotationPresent(CreateBy.class)
&& SqlCommandType.INSERT == sqlCommandType
&& currentUser != null) {
field.set(parameter, currentUser);
field.set(object, currentUser);
}
if (field.isAnnotationPresent(CreateTime.class) && SqlCommandType.INSERT == sqlCommandType) {
field.set(parameter, timestamp);
field.set(object, timestamp);
}
if (field.isAnnotationPresent(UpdateBy.class) && currentUser != null) {
field.set(parameter, currentUser);
field.set(object, currentUser);
}
if (field.isAnnotationPresent(UpdateTime.class)) {
field.set(parameter, timestamp);
field.set(object, timestamp);
}
field.setAccessible(accessible);
}
}

return invocation.proceed();
}
}