MyTetra Share
Делитесь знаниями!
Копия КЛ
Время создания: 11.02.2021 10:21
Раздел: INFO - JOB - rtneo - Работа над задачами
Запись: wwwlir/Tetra/master/base/16013712476n6eqaqkab/text.html на raw.githubusercontent.com

import com.haulmont.cuba.core.global.*

import com.groupstp.rtneo.entity.*

import com.groupstp.rtneo.service.*

import com.groupstp.rtneo.core.bean.*

import java.util.stream.Collectors;

import java.util.stream.Stream;

import com.haulmont.cuba.core.global.ViewRepository;

import com.haulmont.cuba.core.global.View;

import java.util.function.*;


ViewRepository vRep = AppBeans.get(ViewRepository.NAME)

CalculationWorker calculationWorker = AppBeans.get(CalculationWorker.NAME)


MetadataTools tools = metadata.getTools()

CommitContext ctx = new CommitContext()

//From

def fromContragent = dataManager.getReference(Contragent.class, UUID.fromString('1e66bd75-e46f-571b-ef00-9e19a0708556'))

//To

def toContragent = dataManager.getReference(Contragent.class, UUID.fromString('68c368d6-f2be-7ebd-d73e-fdbb5ca1caae'))


HashMap<UUID, UUID> assocCache = new HashMap<>()

UnaryOperator<UUID> assocUUID = {e->

assocCache.put(e, UUID.randomUUID())

return assocCache.get(e)

}

BiFunction<Class, UUID, Object> cacheEntity = {c,u->

dataManager.getReference(c, assocCache.get(u))

}


def waste = dataManager.load(WasteGenerationProject.class)

.query('select w from rtneo$WasteGenerationProject w where w.contragent.id = :contragent_id')

.view("wasteGenerationProject-edit")

.parameter("contragent_id", fromContragent.getId())

.list().stream()

.map{e-> tools.copy(e)}

.peek{e->

e.setId(assocUUID.apply(e.getId()))

e.setContragent(toContragent)

e.setRealEstates(new ArrayList<>())

}.collect(Collectors.toList())


def cresContragent = dataManager.load(ContragentRealEstate.class)

.query('select distinct c from rtneo$ContragentRealEstate c where c.contragent.id = :contragent_id')

// .maxResults(1)

.parameter('contragent_id', fromContragent.getId())

.view('contragentRealEstate-copy')

.list().stream()

.map{e-> tools.copy(e)}

.peek{e->

e.setId(assocUUID.apply(e.getId()))

e.setContragent(toContragent)

e.getWasteGenerationProjects().forEach{wa ->

def wp = waste.stream().filter{w -> w.getId().equals(assocCache.get(wa.getId()))}.findFirst().orElse(null)

wp.getRealEstates().add(e)

}

e.getContainerYards().stream().map{cy -> tools.copy(cy)}.peek{cy->

cy.setId(UUID.randomUUID())

cy.setContragentRealEstate(e)

}.forEach{cy->ctx.addInstanceToCommit(cy)}

e.getRenters().stream().map{r -> tools.copy(r)}.peek{r->

r.setId(UUID.randomUUID())

r.setContragent(toContragent)

r.setContragentRealEstate(e)

}.forEach{r->ctx.addInstanceToCommit(r)}

ctx.addInstanceToCommit(e)

}.collect(Collectors.toList())


ctx.getCommitInstances().addAll(waste)

//dataManager.commit(ctx)

//ctx.getCommitInstances().clear()

def accruals = dataManager.load(Accrual.class)

.query('select a from rtneo$Accrual a where a.contragent.id = :contragent_id')

.parameter('contragent_id', fromContragent.getId())

.view("accrual-copy")

.list().stream().collect(Collectors.groupingBy({a->a.getContractPosition().getId()}))


dataManager.load(Contract.class)

.query('select c from rtneo$Contract c where c.contragent.id = :contragent_id order by c.mainContract desc')

.parameter('contragent_id', fromContragent.getId())

.view('contract-copy')

.list().stream()

.map{e-> tools.copy(e)}

.peek{e->

e.setId(assocUUID.apply(e.getId()))

e.setContragent(toContragent)

ctx.addInstanceToCommit(e)

//Проверить что не присвоется null!!!!!!!!!

if(e.getMainContract() != null){

e.setMainContract(cacheEntity.apply(Contract.class, e.getMainContract().getId()))

}else{

e.setNumber(e.getNumber().toString()+"-copy")

// dataManager.commit(e)

// e = dataManager.reload(e, "contract-copy")

}


def ps = e.getPositions().stream().map{p -> tools.copy(p)}.peek{p->

def psAccruals = accruals.get(p.getId())

p.setId(assocUUID.apply(p.getId()))

p.setContragent(toContragent)

p.setContract(e)

if(p.getContragentRealEstate() != null){

p.setContragentRealEstate(cacheEntity.apply(ContragentRealEstate.class, p.getContragentRealEstate().getId()))

}

if(p.getWasteProject() != null){

p.setWasteProject(cacheEntity.apply(WasteGenerationProject.class, p.getWasteProject().getId()))

}

if(p.getContainerYards() != null && p.getContainerYards().size() >0){

def cpcys = p.getContainerYards().stream().map{cpcy -> tools.copy(cpcy)}.peek{cpcy->

cpcy.setId(UUID.randomUUID())

cpcy.setContractPosition(p)

if(cpcy.getContragentRealEstate() != null){

def crecy = cacheEntity.apply(ContragentRealEstate.class, cpcy.getContragentRealEstate().getId())

if(crecy.getId() == null){

cpcy.setContragentRealEstate(null)

}else{

cpcy.setContragentRealEstate(crecy)

}

}

}.collect(Collectors.toList())

p.setContainerYards(cpcys)

ctx.getCommitInstances().addAll(cpcys)

}else{

p.setContainerYards(new ArrayList<>())

}

if(psAccruals != null){

def pAccruals = psAccruals.stream().map{a-> tools.copy(a)}.peek{a->

a.setId(UUID.randomUUID())

a.setContragent(toContragent)

a.setContractPosition(cacheEntity.apply(ContractPosition.class, a.getContractPosition().getId()))

}.collect(Collectors.toList())

p.setAccruals(pAccruals)

ctx.getCommitInstances().addAll(pAccruals)

}else{

p.setAccruals(new ArrayList<>())

}

ctx.addInstanceToCommit(p)

}.collect(Collectors.toList())

e.setPositions(ps)

// ctx.getCommitInstances().addAll(ps)

// ctx.addInstanceToCommit(e)

}.collect(Collectors.toList())


//dataManager.load(Accrual.class)

// .query('select a from rtneo$Accrual a where a.contragent.id = :contragent_id')

// .parameter('contragent_id', fromContragent.getId())

// .view("accrual-copy")

// .list().stream()

// .map{e-> tools.copy(e)}

// .peek{e->

// e.setId(UUID.randomUUID())

// e.setContragent(toContragent)

// e.setContractPosition(cacheEntity.apply(ContractPosition.class, e.getContractPosition().getId()))

// }.forEach{e->ctx.addInstanceToCommit(e)}


dataManager.load(RealEstateRenter.class)

.query('select r from rtneo$RealEstateRenter r where r.contragentRealEstate.contragent.id = :contragent_id')

.parameter('contragent_id', fromContragent.getId())

.view("realEstateRenter-copy")

.list().stream()

.map{e-> tools.copy(e)}

.peek{e->

e.setId(UUID.randomUUID())

e.setContragentRealEstate(cacheEntity.apply(ContragentRealEstate.class, e.getContragentRealEstate().getId()))

def rr = tools.copy(e.getRenterRecord())

rr.setId(UUID.randomUUID())

rr.setLandlord(toContragent)

e.setRenterRecord(rr)

ctx.addInstanceToCommit(rr)

}.forEach{e->ctx.addInstanceToCommit(e)}


//e.setContragent()

//e.setLandLord(null)????

//e.setRenters(new ArrayList<>())

//e.setContainerYards(new ArrayList<>())

//e.setWasteGenerationProjects(new ArrayList<>())


dataManager.commit(ctx)


ctx.getCommitInstances().each{_(it)}

/**

* Логирование

*/

import com.haulmont.cuba.core.app.serialization.EntitySerializationAPI;

import com.groupstp.rtneo.util.JsonUtil;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

private _(Object obj, String... options){

if(obj == null){log("LOG.ERROR: Object is null!!!");return}

if(options.size() == 0){log(obj)}

for(def option : options){

if(option.equals("str")){log(obj)}

if(option.equals("for")){obj.each({_(it)})}

if(option.equals("json")){

EntitySerializationAPI entitySerializationAPI = AppBeans.get(EntitySerializationAPI.NAME)

try{log(entitySerializationAPI.toJson(obj))}

catch(Exception e){log("LOG.ERROR: JSON entity serialization failed")}

}

if(option.equals("objJson")){

JsonUtil jsonUtil = AppBeans.get(JsonUtil.NAME)

try{log.(jsonUtil.toJson(obj))}

catch(Exception e){log("LOG.ERROR: JSON object serialization failed")}

}

if(option.equals("date")){

DateFormat df = new SimpleDateFormat("dd.MM.yyyy");

try{log(df.format(obj))}

catch(Exception e){log("LOG.ERROR: Failed date format")}

}

}

}

private log(Object obj){log.debug(obj)}


Так же в этом разделе:
 
MyTetra Share v.0.65
Яндекс индекс цитирования