MyTetra Share
Делитесь знаниями!
Установка категорий из файла
Время создания: 08.04.2021 09:14
Раздел: INFO - JOB - rtneo - Работа над задачами - Категоризация
Запись: wwwlir/Tetra/master/base/1617844472tqm5zib2nq/text.html на raw.githubusercontent.com

import com.groupstp.rtneo.entity.*;

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

import com.groupstp.rtneo.service.*;

import com.haulmont.cuba.core.sys.SecurityContext;

import com.haulmont.cuba.core.sys.AppContext;

import com.haulmont.cuba.core.entity.FileDescriptor

import com.haulmont.cuba.core.app.FileStorageAPI

import java.io.InputStream

import java.io.InputStreamReader

import java.io.ByteArrayOutputStream

import org.apache.commons.io.IOUtils

import java.nio.charset.StandardCharsets;

import com.opencsv.CSVReader;

import org.apache.commons.collections.CollectionUtils

import java.util.stream.Collectors;

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


HashMap<String, RealEstateCategory> category = categoryInit()


FileStorageAPI fileStorageAPI=AppBeans.get(FileStorageAPI.NAME);


String id='fabf0eb9-cfcd-204c-3cde-e196e89640e1'

FileDescriptor fd = dataManager.load(FileDescriptor.class)

.id(UuidProvider.fromString(id))

.view(View.MINIMAL)

.optional().orElse(null);

if(fd == null){throw new RuntimeException("fileNotFound")}


InputStream inputStream=fileStorageAPI.openStream(fd)

CSVReader reader = new CSVReader(new InputStreamReader(inputStream, "UTF-8"));


List<String> list = reader.readAll().stream().skip(1)

// .map{e->e[0]}

.collect(Collectors.toList())

//list.each{_(it)}


ViewRepository vRep = AppBeans.get(ViewRepository.NAME)

View creView = vRep.getView(ContragentRealEstate.class, "_local").addProperty("category", vRep.getView(RealEstateCategory.class, "_local"))


for(def item : list){

List<ContragentRealEstate> cres = dataManager.load(ContragentRealEstate.class)

.query('select cre from rtneo$ContragentRealEstate cre where cre.contragent.personalAccount = :pa and cre.realEstate.cadastralNumber = :cad and (select count(s) from sec$SessionLogEntry s where s.user.login = cre.contragent.user.login) = 0 and (select count(p) from rtneo$Payment p where p.inn = cre.contragent.inn) = 0')

.parameter("pa", item[2])

.parameter("cad", item[0])

.view(creView)

.list()

RealEstateCategory reCat = category.get(item[3])

cres.stream()

.peek{_(it)}

.forEach{

it.setCategory(reCat)

// dataManager.commit(it)

}

}


HashMap<String, RealEstateCategory> categoryInit(){

def categories = ["продукты": "99035938-4cb2-7805-137c-463135af7b44",

"склад": "e55b4b8a-4540-7917-7e4f-9259fe8ecf24",

"баня": "341d6981-1d3f-40f0-1a5d-35e40dd6dcd2",

"жилой дом": "98f062f7-f447-0720-103c-f16e1dfce2f5",

"гараж": "a0c51642-1951-4f39-b432-7a870e3791a2",

"Жилое в СНТ": "e043c039-7061-4330-5e3a-6d76372d4b14",

"спецтовары": "5c70978f-2e47-6cd7-b7bc-f3ed125abdcb",

"гостиница": "19ddea75-efdc-1183-bceb-079dd66cd95e",

"офис": "0a64596e-af08-5239-1b01-3c7bf7481f54",

"Нежилое в СНТ": "258aab55-815a-ae9b-c2a7-630e85262263",

"кафе": "3fe1c3f2-6d0f-4db1-c72d-7449709e4c98",

"салон красоты": "51fd362e-596b-ca2e-6054-c5850f3fc776",

"промтовары": "e171f083-544f-3e7c-c573-b89cbffc54ff",

"иные": "2d7b41c2-0090-26f4-6284-da0931709a03",

"автостоянка": "61ae9a56-6be5-6261-1f51-b4177aa904c3",

"СНТ": "258aab55-815a-ae9b-c2a7-630e85262263",

"образовательные": "18d6cbcd-7ccb-3bea-f2ee-865e659f3e1c",

"квартира": "eab380cd-b05e-6117-ebe2-7ae34ac3542b",

"СТО": "beb8d1b1-9726-9fb3-1932-f98472999f63",

"столовая": "b2ce997c-b1da-2694-e804-4ce733f4449a",

"производство": "183491eb-9ae2-1f69-6f96-0cb88d4895c2",

"химчистка": "39c088ea-b886-59a0-cf74-97ccf83933ea",

"технические": "11e5097b-7f00-27c8-f989-3cb3d80a2f55"]

HashMap<String, Category> cat = [:]

categories.entrySet()

.stream()

.forEach{

cat.put(it.getKey(), dataManager.load(RealEstateCategory.class).id(UUID.fromString(it.getValue())).one())

}

return cat;

}


/**

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

*/

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 log2(Object obj){log.debug(obj)}

private log(Object obj){log.result.append("${obj}\n")}

 
MyTetra Share v.0.65
Яндекс индекс цитирования