2022-05-13 18:42:11 +02:00
|
|
|
package org.gcube.application.cms.usecases;
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2022-11-21 20:51:15 +01:00
|
|
|
import org.bson.Document;
|
2022-05-13 18:42:11 +02:00
|
|
|
import org.gcube.application.cms.tests.TokenSetter;
|
|
|
|
import org.gcube.application.geoportal.common.model.document.Project;
|
|
|
|
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
|
|
|
|
import org.gcube.application.geoportal.common.rest.Projects;
|
2022-11-21 20:51:15 +01:00
|
|
|
import org.gcube.application.geoportal.common.utils.tests.GCubeTest;
|
2022-05-13 18:42:11 +02:00
|
|
|
|
|
|
|
import java.rmi.RemoteException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
2022-09-26 16:11:20 +02:00
|
|
|
import java.util.concurrent.ConcurrentSkipListSet;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2022-05-13 18:42:11 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
|
|
|
|
import static org.gcube.application.geoportal.client.plugins.GeoportalAbstractPlugin.projects;
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
public class ClearProjects {
|
|
|
|
|
|
|
|
public static void main(String[] args) throws RemoteException, InterruptedException {
|
2022-11-21 20:51:15 +01:00
|
|
|
// String context="/gcube/devsec/devVRE";
|
2022-05-13 18:42:11 +02:00
|
|
|
|
2022-11-21 20:51:15 +01:00
|
|
|
// String context="/pred4s/preprod/preVRE";
|
2022-05-13 18:42:11 +02:00
|
|
|
|
2022-11-21 20:51:15 +01:00
|
|
|
TokenSetter.set(GCubeTest.getContext());
|
2022-05-13 18:42:11 +02:00
|
|
|
|
2022-12-13 16:52:25 +01:00
|
|
|
Projects<Project> client=projects("basic").build();;
|
2022-05-13 18:42:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
ArrayList<String> toSkipIds=new ArrayList<>();
|
|
|
|
// toSkipIds.add("6102c8dd02ad3d05b5f81df4");
|
|
|
|
// toSkipIds.add("610415af02ad3d05b5f81ee3");
|
|
|
|
|
|
|
|
AtomicLong count=new AtomicLong(0);
|
|
|
|
AtomicLong nullCount=new AtomicLong(0);
|
2022-05-17 15:43:19 +02:00
|
|
|
|
|
|
|
ConcurrentSkipListSet<String> errors = new ConcurrentSkipListSet<>();
|
|
|
|
|
2022-05-13 18:42:11 +02:00
|
|
|
AtomicLong found=new AtomicLong(0);
|
|
|
|
|
|
|
|
Iterator<Project> it=null;
|
2022-11-21 20:51:15 +01:00
|
|
|
|
|
|
|
QueryRequest q = new QueryRequest();
|
2022-11-23 14:31:36 +01:00
|
|
|
|
|
|
|
// String queryString = String.format("{\"_theDocument.nome\" :{\"$eq\" : \"Landro (Comune di Tambre, BL) \"},\"_theDocument.dataInizioProgetto\":{\"$eq\":\"2021-05-29T00:00:00\"}}\n" +
|
|
|
|
// "}");
|
|
|
|
// q.setFilter(Document.parse(queryString));
|
|
|
|
//
|
2022-11-21 20:51:15 +01:00
|
|
|
it=client.query(q);
|
|
|
|
|
2022-05-13 18:42:11 +02:00
|
|
|
|
2022-05-17 15:43:19 +02:00
|
|
|
ExecutorService service = Executors.newFixedThreadPool(1);
|
2022-05-13 18:42:11 +02:00
|
|
|
|
2022-06-08 18:12:11 +02:00
|
|
|
ConcurrentSkipListSet<String> ids = new ConcurrentSkipListSet<>();
|
|
|
|
|
|
|
|
// it.forEachRemaining(project -> ids.add(project.getId()));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-05-13 18:42:11 +02:00
|
|
|
|
|
|
|
it.forEachRemaining((Project c)->{
|
2022-06-08 18:12:11 +02:00
|
|
|
System.out.println("Found ID "+c.getId()+" N° "+found.incrementAndGet());
|
|
|
|
|
2022-05-13 18:42:11 +02:00
|
|
|
service.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try{
|
2022-11-21 20:51:15 +01:00
|
|
|
TokenSetter.set(GCubeTest.getContext());
|
2022-05-13 18:42:11 +02:00
|
|
|
String currentId=c.getId();
|
|
|
|
if(currentId==null) {
|
|
|
|
System.out.println("ID IS NULL " + c);
|
|
|
|
nullCount.incrementAndGet();
|
|
|
|
}
|
2022-05-17 15:43:19 +02:00
|
|
|
if(c.getLock()!=null){
|
|
|
|
System.out.println("Unlocking "+currentId);
|
|
|
|
client.forceUnlock(currentId);
|
|
|
|
}
|
|
|
|
|
2022-05-13 18:42:11 +02:00
|
|
|
else
|
|
|
|
if(toSkipIds.contains(currentId))
|
|
|
|
System.out.println("Skipping "+currentId);
|
|
|
|
else {
|
|
|
|
System.out.println("Deleting " + c.getId());
|
|
|
|
client.deleteById(c.getId(),true);
|
|
|
|
}
|
|
|
|
}catch(Throwable throwable){
|
|
|
|
System.err.println(throwable);
|
2022-05-17 15:43:19 +02:00
|
|
|
errors.add(c.getId());
|
2022-06-08 18:12:11 +02:00
|
|
|
// try {Thread.sleep(1000);} catch (InterruptedException i) {}
|
|
|
|
}finally{
|
|
|
|
System.out.println("Handled N "+count.incrementAndGet()+" concessioni");
|
2022-05-13 18:42:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
while (!service.awaitTermination(1, TimeUnit.MINUTES)) {
|
|
|
|
log.info("Waiting .. completed {}, out of {} ",count.get(),found.get());
|
|
|
|
if(found.get()==count.get()) service.shutdown();
|
|
|
|
}
|
|
|
|
|
2022-05-17 15:43:19 +02:00
|
|
|
System.out.println("Done "+count.get()+" [null : "+nullCount.get()+", err : "+errors.size()+"]");
|
2022-05-13 18:42:11 +02:00
|
|
|
|
2022-05-17 15:43:19 +02:00
|
|
|
System.out.println("Errors : ");
|
|
|
|
errors.forEach(s -> System.out.println(s));
|
2022-05-13 18:42:11 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|