|
@@ -5,17 +5,26 @@ import com.kgraph.graph.neo4j.DTO.EntityDTO;
|
|
|
import com.kgraph.graph.neo4j.domain.Entity;
|
|
|
import com.kgraph.graph.neo4j.mapper.EntityRepository;
|
|
|
import com.kgraph.graph.neo4j.service.INeo4jEntityService;
|
|
|
+import org.neo4j.ogm.session.Session;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
@Service
|
|
|
public class Neo4JNeo4jEntityServiceImpl implements INeo4jEntityService {
|
|
|
|
|
|
@Autowired
|
|
|
EntityRepository repository;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private Session session;
|
|
|
+
|
|
|
@Override
|
|
|
public Iterable<Entity> findAll() {
|
|
|
return repository.findAll();
|
|
@@ -28,9 +37,32 @@ public class Neo4JNeo4jEntityServiceImpl implements INeo4jEntityService {
|
|
|
|
|
|
@Override
|
|
|
public Entity save(Entity entity) {
|
|
|
+ deleteLabel(entity);
|
|
|
return repository.save(entity);
|
|
|
}
|
|
|
|
|
|
+ private void deleteLabel(Entity entity) {
|
|
|
+ Long id = entity.getId();
|
|
|
+ if (id != null) {
|
|
|
+ Entity entityDb = this.findById(id);
|
|
|
+ List<String> oldLabels = entityDb.getLabels();
|
|
|
+ List<String> newLabels = entity.getLabels();
|
|
|
+ List<String> difference = new ArrayList<>(oldLabels);
|
|
|
+ difference.removeAll(newLabels);
|
|
|
+ difference.remove(Entity.class.getName());
|
|
|
+ for (String deleteLabel : difference) {
|
|
|
+ removeLabelFromNode(id, deleteLabel);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void removeLabelFromNode(Long nodeId, String labelName) {
|
|
|
+ String query = "MATCH (n) WHERE id(n) = $nodeId REMOVE n:" + labelName;
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("nodeId", nodeId);
|
|
|
+ session.query(query, params);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void delete(Long[] ids){
|
|
|
// todo optimize
|