Skip to content

Commit

Permalink
Cappability mapping not imported if Level<3
Browse files Browse the repository at this point in the history
  • Loading branch information
mauvaisetroupe committed Nov 5, 2023
1 parent 41ffd05 commit bac028e
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
import com.mauvaisetroupe.eadesignit.repository.CapabilityRepository;
import com.mauvaisetroupe.eadesignit.service.dto.mapper.CapabilityMapper;
import com.mauvaisetroupe.eadesignit.service.importfile.dto.CapabilityImportDTO;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.List;

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

@Component
public class CapabilityUtil {

@Autowired
CapabilityRepository capabilityRepository;

Expand All @@ -33,7 +31,7 @@ public Capability buildCapabilityTree(Collection<Capability> inputs) {
public List<Capability> buildCapabilityTreeWithoutRoot(Collection<Capability> inputs) {
List<Capability> listOfRoots = buildCapabilityTree(inputs, false);
return listOfRoots;
}
}

private List<Capability> buildCapabilityTree(Collection<Capability> inputs, boolean includeRoots) {
// Merge all capabilities finding common parents
Expand All @@ -44,9 +42,9 @@ private List<Capability> buildCapabilityTree(Collection<Capability> inputs, bool
List<Capability> listOfRoots = new ArrayList<>();

if (inputs == null || inputs.isEmpty()) return listOfRoots;

CapabilityMapper mapper = new CapabilityMapper();
// Fullpath is not a solution for key,
// Fullpath is not a solution for key,
Map<String, Capability> capabilityByFullpath = new HashMap<>();
Map<String, Capability> capabilityByFullpathAddedInTree = new HashMap<>();

Expand Down Expand Up @@ -80,32 +78,30 @@ private List<Capability> buildCapabilityTree(Collection<Capability> inputs, bool
}
if (!includeRoots) {
Assert.isTrue(capabilityByFullpathAddedInTree.size() == inputs.size(), "should have the same size");
}
}
return listOfRoots;
}

private void addToRootList(List<Capability> listOfRoots, Capability dto) {
// COMMENT SAVOIR SI CEST UN ROOT LOCAL ?
if (dto != null) {
if (listOfRoots.isEmpty()) {
listOfRoots.add(dto);
}
else {
listOfRoots.add(dto);
} else {
Iterator<Capability> i = listOfRoots.iterator();
boolean stopProcessing = false;
while (i.hasNext() && !stopProcessing) {
Capability rootCapability = i.next();
while (i.hasNext() && !stopProcessing) {
Capability rootCapability = i.next();
if (getCapabilityFullPath(rootCapability).equals(getCapabilityFullPath(dto))) {
stopProcessing = true;
}
else if (getCapabilityFullPath(rootCapability).contains(getCapabilityFullPath(dto) + " > ")) {
stopProcessing = true;
} else if (getCapabilityFullPath(rootCapability).contains(getCapabilityFullPath(dto) + " > ")) {
// root is a child, replace root by DTO
i.remove();
listOfRoots.add(dto);
listOfRoots.add(dto);
stopProcessing = true;
} else if (getCapabilityFullPath(dto).contains(getCapabilityFullPath(rootCapability) + " > " )) {
} else if (getCapabilityFullPath(dto).contains(getCapabilityFullPath(rootCapability) + " > ")) {
// dto is a child, we keep root and ignore dto
stopProcessing = true;
stopProcessing = true;
}
}
if (!stopProcessing) {
Expand All @@ -114,7 +110,7 @@ else if (getCapabilityFullPath(rootCapability).contains(getCapabilityFullPath(dt
}
}
}

public boolean contains(Collection<Capability> inputs, Capability tmpCapability) {
if (tmpCapability == null) return false;
for (Capability capability : inputs) {
Expand All @@ -132,39 +128,44 @@ public String getCapabilityFullPath(Capability capability) {
throw new IllegalStateException("Capability hah itself for parent");
}
if (tmCapability.getParent() == null) {
Assert.isTrue(tmCapability.getName().equals("ROOT"), "Cannot compute full path if parents are not pessent until ROOT " + capability);
Assert.isTrue(
tmCapability.getName().equals("ROOT"),
"Cannot compute full path if parents are not pessent until ROOT " + capability
);
}
buffer.insert(0, sep).insert(0, tmCapability.getName());
tmCapability = tmCapability.getParent();
sep = " > ";
}
return buffer.toString();
}
}

public String getCapabilityFullPath(CapabilityImportDTO importDTO) {
public String getCapabilityFullPath(CapabilityImportDTO importDTO, boolean replaceNullInPath) {
StringBuilder buffer = new StringBuilder();
String sep = "";
for (Capability capability : importDTO.getCapabilityList()) {
buffer.append(sep).append(capability != null ? capability.getName() : " --- ");
sep = " > ";
if (replaceNullInPath || capability != null) {
buffer.append(sep).append(capability != null ? capability.getName() : " --- ");
}
sep = " > ";
}
return buffer.toString();
}
}

public Map<String,Capability> initCapabilitiesByNameFromDB() {
Map<String,Capability> capabilitiesByFllPath = new HashMap<>();
public Map<String, Capability> initCapabilitiesByNameFromDB() {
Map<String, Capability> capabilitiesByFllPath = new HashMap<>();
List<Capability> allCapabilities = capabilityRepository.findAllWithSubCapabilities();
for (Capability capability : allCapabilities) {
capabilitiesByFllPath.put(getCapabilityFullPath(capability), capability);
}
return capabilitiesByFllPath;
}
}
return capabilitiesByFllPath;
}

public CapabilityImportDTO getCapabilityImportDTO(String fullPath) {
Capability domain = null, l0Import = null, l1Import = null, l2Import = null, l3Import = null;
String[] capabilitiesName = fullPath.split(" > ");
Capability root = new Capability("ROOT", -2);
if (capabilitiesName.length > 0) domain = new Capability(capabilitiesName[0], -1);
if (capabilitiesName.length > 0) domain = new Capability(capabilitiesName[0], -1);
if (capabilitiesName.length > 1) l0Import = new Capability(capabilitiesName[1], 0);
if (capabilitiesName.length > 2) l1Import = new Capability(capabilitiesName[2], 1);
if (capabilitiesName.length > 3) l2Import = new Capability(capabilitiesName[3], 2);
Expand All @@ -174,20 +175,24 @@ public CapabilityImportDTO getCapabilityImportDTO(String fullPath) {
}

public CapabilityImportDTO buildImportDTO(Capability capability, String error, ImportStatus status) {
Capability[] capabilities = {null, null, null, null, null, null};
capabilities[0] = new Capability("ROOT",-2);
Capability[] capabilities = { null, null, null, null, null, null };
capabilities[0] = new Capability("ROOT", -2);
Capability tmp = capability;
for (int level = capability.getLevel(); level >= 0 ; level--) {
capabilities[level+2] = tmp;
tmp = capability.getParent();
}
for (int level = capability.getLevel(); level >= 0; level--) {
capabilities[level + 2] = tmp;
tmp = capability.getParent();
}

CapabilityImportDTO capabilityImportDTO = new CapabilityImportDTO(
capabilities[0], capabilities[1], capabilities[2], capabilities[3], capabilities[4], capabilities[5]
capabilities[0],
capabilities[1],
capabilities[2],
capabilities[3],
capabilities[4],
capabilities[5]
);
capabilityImportDTO.setError(error);
capabilityImportDTO.setStatus(status);
return capabilityImportDTO;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,17 @@ public List<ApplicationCapabilityItemDTO> importExcel(InputStream excel, String

// remove mapping from landscape and delete CapabilityMapping if not refernced by another landscape
Set<CapabilityApplicationMapping> capabilityApplicationMappings = new HashSet<>(landscape.getCapabilityApplicationMappings());
for (CapabilityApplicationMapping cm : capabilityApplicationMappings) {

for (CapabilityApplicationMapping cm : capabilityApplicationMappings) {
// remove capabilityMapping from landscape
landscape.removeCapabilityApplicationMapping(cm);

// if capabiltyMapping have no other landscape, delete it
if (cm.getLandscapes() == null || cm.getLandscapes().isEmpty()) {
capabilityApplicationMappingRepository.delete(cm);
}

}

Map<String,Capability> capabilitiesByFullPath = capabilityUtil.initCapabilitiesByNameFromDB();
Map<String, Capability> capabilitiesByFullPath = capabilityUtil.initCapabilitiesByNameFromDB();

List<Map<String, Object>> capabilitiesDF = capabilityFlowExcelReader.getSheet(sheetname);
for (Map<String, Object> map : capabilitiesDF) {
Expand All @@ -86,7 +84,7 @@ public List<ApplicationCapabilityItemDTO> importExcel(InputStream excel, String
itemDTO.setApplicationNames(mapArrayToString(map));

// fullpath from ecel does not inclute ROOT >
Capability capability = capabilitiesByFullPath.get(capabilityUtil.getCapabilityFullPath(capabilityImportDTO));
Capability capability = capabilitiesByFullPath.get(capabilityUtil.getCapabilityFullPath(capabilityImportDTO, false));
List<Application> applications = findApplication(map, itemDTO);

if (applications.isEmpty()) {
Expand All @@ -101,10 +99,8 @@ public List<ApplicationCapabilityItemDTO> importExcel(InputStream excel, String
log.error(error);
} else {
for (Application application : applications) {
CapabilityApplicationMapping capabilityApplicationMapping = capabilityApplicationMappingRepository.findByApplicationAndCapability(
application,
capability
);
CapabilityApplicationMapping capabilityApplicationMapping =
capabilityApplicationMappingRepository.findByApplicationAndCapability(application, capability);
if (capabilityApplicationMapping == null) {
capabilityApplicationMapping = new CapabilityApplicationMapping();
capabilityApplicationMappingRepository.save(capabilityApplicationMapping);
Expand Down
Loading

0 comments on commit bac028e

Please sign in to comment.