com.invms.x.repositories.basis.BasisRepository Maven / Gradle / Ivy
package com.invms.x.repositories.basis;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.invms.x.builders.ArgsBuilder;
import com.invms.x.builders.EntityBuilder;
import com.invms.x.builders.QueryBuilder;
import com.invms.x.components.DefaultConfigurer;
import com.invms.x.components.DefaultException;
import com.invms.x.entities.IIdedEntity;
import com.invms.x.entities.basis.PowerEntity;
import com.invms.x.filters.EntityFilter;
import com.invms.x.filters.FieldFilter;
import com.invms.x.repositories.BaseRepository;
import com.invms.x.repositories.EntityRepository;
import com.invms.x.utils.ComUtils;
import com.invms.x.utils.XUtils;
@Repository
public class BasisRepository extends BaseRepository {
@Autowired
private EntityRepository entityRepository;
protected String buildIdQuery(String... ids) {
String query;
if (ids.length > 1) {
query = "in ('" + ComUtils.join("', '", (Object[]) ids) + "')";
} else {
query = "= '" + ids[0] + "'";
}
return query;
}
protected String buildUserPowerQuery(String field, String subCategory, String... userIds) {
StringBuilder stringBuilder = new StringBuilder();
String userQuery = buildIdQuery(userIds);
stringBuilder.append("(" + field + " in (select userPower.id.targetId from PowerEntity userPower");
stringBuilder.append(" where userPower.id.category = 'user_" + subCategory + "' and userPower.id.sourceId " + userQuery + ")");
if (!"Role".equals(subCategory)) {
stringBuilder.append(" or " + field + " in (select rolePower.id.targetId from RoleEntity role, PowerEntity userRole, PowerEntity rolePower");
stringBuilder.append(" where userRole.id.category = 'user_role' and userRole.id.sourceId " + userQuery + " and userRole.id.targetId = role.id");
stringBuilder.append(" and rolePower.id.category = 'role_" + subCategory + "' and rolePower.id.sourceId = role.id)");
}
stringBuilder.append(")");
return stringBuilder.toString();
}
protected String buildRolePowerQuery(String field, String subCategory, String... roleIds) {
StringBuilder stringBuilder = new StringBuilder();
String roleQuery = buildIdQuery(roleIds);
stringBuilder.append("(" + field + " in (select rolePower.id.targetId from PowerEntity rolePower");
stringBuilder.append(" where rolePower.id.category = 'role_" + subCategory + "' and rolePower.id.sourceId " + roleQuery + ")");
stringBuilder.append(")");
return stringBuilder.toString();
}
public List getPowersByUserIds(Class resultClass, Class> entityClass, String... userIds) throws Exception {
String subCategory = XUtils.buildCode(entityClass.getSimpleName());
QueryBuilder queryBuilder = new QueryBuilder();
queryBuilder.addQuery(buildUserPowerQuery("t.id", subCategory, userIds));
buildQuery(resultClass, entityClass, EntityFilter.METHOD_RANGE, queryBuilder, null, null, false, false);
String query = queryBuilder.getQuery();
Object[] args = queryBuilder.getArgs();
EntityBuilder entityBuilder = createEntityBuilder();
return entityBuilder.select(resultClass, entityClass, query, args);
}
public List getPowersByRoleIds(Class resultClass, Class> entityClass, String... roleIds) throws Exception {
String subCategory = XUtils.buildCode(entityClass.getSimpleName());
QueryBuilder queryBuilder = new QueryBuilder();
queryBuilder.addQuery(buildRolePowerQuery("t.id", subCategory, roleIds));
buildQuery(resultClass, entityClass, EntityFilter.METHOD_RANGE, queryBuilder, null, null, false, false);
String query = queryBuilder.getQuery();
Object[] args = queryBuilder.getArgs();
EntityBuilder entityBuilder = createEntityBuilder();
return entityBuilder.select(resultClass, entityClass, query, args);
}
public , TTarget extends IIdedEntity>> void setPowerEntities(TSource source, List newTargets, List oldTargets, String category) throws Exception {
verifyManyToMany(oldTargets, newTargets, new Predicate() {
@Override
public boolean test(TTarget entity) {
Object id = entity.getId();
for (TTarget item : oldTargets) {
if (ComUtils.equal(id, item.getId())) {
return false;
}
}
return true;
}
}, new Function() {
@Override
public Exception apply(TTarget target) {
try {
PowerEntity power = new PowerEntity();
PowerEntity.Id id = power.getId();
id.setSourceId(ComUtils.parse(String.class, source.getId()));
id.setTargetId(ComUtils.parse(String.class, target.getId()));
id.setCategory(category);
entityRepository.insert(power);
return null;
} catch (Exception e) {
return e;
}
}
}, new Predicate() {
@Override
public boolean test(TTarget target) {
Object id = target.getId();
for (TTarget item : newTargets) {
if (ComUtils.equal(id, item.getId())) {
return false;
}
}
return true;
}
}, new Function() {
@Override
public Exception apply(TTarget target) {
try {
PowerEntity power = entityRepository.getResultByFields(PowerEntity.class, PowerEntity.class, new String[] { "id.sourceId", "id.targetId", "id.category" }, new Object[] { source.getId(), target.getId(), category });
if (power != null) {
entityRepository.delete(power);
}
return null;
} catch (Exception e) {
return e;
}
}
});
}
public , TTarget extends IIdedEntity>> void setPowerEntities(Class sourceClass, Class targetClass, String sourceId, Object[] newTargetIds, List oldTargets, String category) throws Exception {
TSource source;
if (ComUtils.empty(sourceId) || (source = entityRepository.getResultByFields(sourceClass, sourceClass, "id", sourceId)) == null) {
FieldFilter fieldFilter = sourceClass.getAnnotation(FieldFilter.class);
String prefix = fieldFilter == null ? XUtils.buildCode(sourceClass.getSimpleName()) : fieldFilter.name();
throw new DefaultException(String.format("%s 不存在", prefix));
}
List newTargets = entityRepository.getResultsByFields(targetClass, targetClass, "id", newTargetIds);
setPowerEntities(source, newTargets, oldTargets, category);
}
public > void setLoggedPower(TEntity entity, ArgsBuilder argsBuilder) throws Exception {
String sourceId = argsBuilder.getValue(String.class, DefaultConfigurer.loggedId);
String targetId = ComUtils.parse(String.class, entity.getId());
if (!ComUtils.empty(targetId)) {
PowerEntity power = new PowerEntity();
PowerEntity.Id id = power.getId();
id.setSourceId(sourceId);
id.setTargetId(targetId);
id.setCategory("user_" + XUtils.buildCode(entity.getClass().getSimpleName()));
entityRepository.insert(power);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy