use of com.qcadoo.view.api.ViewDefinitionState in project mes by qcadoo.
the class WarehouseIssueDetailsListeners method fillProductsToIssue.
public void fillProductsToIssue(final ViewDefinitionState view, final ComponentState state, final String[] args) {
FormComponent issueForm = (FormComponent) view.getComponentByReference(QcadooViewConstants.L_FORM);
FieldComponent collectionProductsField = (FieldComponent) view.getComponentByReference(WarehouseIssueFields.COLLECTION_PRODUCTS);
LookupComponent operation = (LookupComponent) view.getComponentByReference(WarehouseIssueFields.TECHNOLOGY_OPERATION_COMPONENT);
LookupComponent division = (LookupComponent) view.getComponentByReference(WarehouseIssueFields.DIVISION);
Long warehouseIssueId = issueForm.getEntityId();
Entity toc = operation.getEntity();
Entity divisionEntity = division.getEntity();
List<Entity> createdProducts = warehouseIssueService.fillProductsToIssue(warehouseIssueId, CollectionProducts.fromStringValue(collectionProductsField.getFieldValue().toString()), toc, divisionEntity);
if (createdProducts != null) {
List<Entity> invalidProducts = createdProducts.stream().filter(productToIssue -> !productToIssue.isValid()).collect(Collectors.toList());
if (invalidProducts.isEmpty()) {
view.addMessage("productFlowThruDivision.issue.downloadedProducts", ComponentState.MessageType.SUCCESS);
} else {
Multimap<String, String> errors = ArrayListMultimap.create();
for (Entity productToIssue : invalidProducts) {
String number = productToIssue.getBelongsToField(ProductsToIssueFields.PRODUCT).getStringField(ProductFields.NUMBER);
Map<String, ErrorMessage> errorMessages = productToIssue.getErrors();
errorMessages.entrySet().stream().forEach(entry -> errors.put(entry.getValue().getMessage(), number));
productToIssue.getGlobalErrors().stream().forEach(error -> errors.put(error.getMessage(), number));
}
view.addMessage("productFlowThruDivision.issue.downloadedProductsError", ComponentState.MessageType.INFO, false);
for (String message : errors.keySet()) {
String translatedMessage = translationService.translate(message, LocaleContextHolder.getLocale());
String products = errors.get(message).stream().collect(Collectors.joining(", "));
if ((translatedMessage + products).length() < 255) {
view.addMessage("productFlowThruDivision.issue.downloadedProductsErrorMessages", ComponentState.MessageType.FAILURE, false, translatedMessage, products);
}
}
}
} else {
view.addMessage("productFlowThruDivision.issue.noProductsToDownload", ComponentState.MessageType.INFO);
}
}
use of com.qcadoo.view.api.ViewDefinitionState in project mes by qcadoo.
the class RemoveTOCService method removeOnlySelectedOperation.
@Transactional
public boolean removeOnlySelectedOperation(final Entity tocToDelete, final ViewDefinitionState view) {
List<Entity> usageInProductStructureTree = dataDefinitionService.get(TechnologiesConstants.PLUGIN_IDENTIFIER, TechnologiesConstants.MODEL_PRODUCT_STRUCTURE_TREE_NODE).find().add(SearchRestrictions.belongsTo(ProductStructureTreeNodeFields.OPERATION, tocToDelete)).list().getEntities();
if (!usageInProductStructureTree.isEmpty()) {
view.addMessage("technologies.technologyDetails.window.treeTab.technologyTree.error.cannotDeleteOperationUsedInProductStructureTree", ComponentState.MessageType.FAILURE, false, usageInProductStructureTree.stream().map(e -> e.getBelongsToField(ProductStructureTreeNodeFields.MAIN_TECHNOLOGY).getStringField(TechnologyFields.NUMBER)).distinct().collect(Collectors.joining(", ")));
return false;
}
final Entity parent = tocToDelete.getBelongsToField(TechnologyOperationComponentFields.PARENT);
final List<Entity> operationsToRewrite = tocToDelete.getHasManyField(TechnologyOperationComponentFields.CHILDREN);
if (parent == null) {
if (operationsToRewrite.size() > 1) {
view.addMessage("technologies.technologyDetails.window.treeTab.technologyTree.error.cannotDeleteRoot", ComponentState.MessageType.FAILURE);
return false;
} else if (operationsToRewrite.size() == 1) {
return createNewRoot(operationsToRewrite.get(0), tocToDelete, view);
} else {
deleteOldToc(tocToDelete);
return true;
}
}
final Optional<Entity> mainOutProductComponentToDelete = technologyService.tryGetMainOutputProductComponent(tocToDelete);
final List<Entity> originalInProducts = tocToDelete.getHasManyField(TechnologyOperationComponentFields.OPERATION_PRODUCT_IN_COMPONENTS);
List<Entity> newInProducts = Lists.newArrayList();
for (Entity toc : operationsToRewrite) {
Optional<Entity> mainOutProductComponent = technologyService.tryGetMainOutputProductComponent(toc);
if (mainOutProductComponent.isPresent()) {
Optional<Entity> maybeInProductComponent = getInProductComponentFromProductComponent(originalInProducts, mainOutProductComponent.get());
if (maybeInProductComponent.isPresent()) {
Entity inProductComponent = maybeInProductComponent.get();
newInProducts.add(inProductComponent);
}
}
setNewParent(toc, parent);
}
if (!rewriteInProductComponents(parent, newInProducts, mainOutProductComponentToDelete, view)) {
view.addMessage("technologies.technologyDetails.window.treeTab.technologyTree.error.cannotRewriteProducts", ComponentState.MessageType.FAILURE);
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return false;
}
deleteOldToc(tocToDelete);
return true;
}
use of com.qcadoo.view.api.ViewDefinitionState in project qcadoo by qcadoo.
the class ExportToCsvController method generateCsv.
@Monitorable(threshold = 500)
@ResponseBody
@RequestMapping(value = { L_CONTROLLER_PATH }, method = RequestMethod.POST)
public Object generateCsv(@PathVariable(L_PLUGIN_IDENTIFIER_VARIABLE) final String pluginIdentifier, @PathVariable(L_VIEW_NAME_VARIABLE) final String viewName, @RequestBody final JSONObject body, final Locale locale, @RequestHeader("User-Agent") final String userAgent) {
try {
changeMaxResults(body);
ViewDefinitionState state = crudService.invokeEvent(pluginIdentifier, viewName, body, locale);
GridComponent grid = (GridComponent) state.getComponentByReference(QcadooViewConstants.L_GRID);
if (grid == null) {
JSONArray args = body.getJSONObject("event").getJSONArray("args");
if (args.length() > 0) {
grid = (GridComponent) state.getComponentByReference(args.getString(0));
}
}
List<String> columns = getColumns(grid);
List<String> columnNames = getColumnNames(grid, columns);
List<Map<String, String>> rows;
if (grid.getSelectedEntitiesIds().isEmpty()) {
rows = grid.getColumnValuesOfAllRecords();
} else {
rows = grid.getColumnValuesOfSelectedRecords();
}
File file = exportToCsv.createExportFile(columns, columnNames, rows, grid.getName());
boolean openInNewWindow = !StringUtils.isNoneBlank(userAgent) || (!userAgent.contains("Chrome") && !userAgent.contains("Safari")) || userAgent.contains("Edge");
state.redirectTo(fileService.getUrl(file.getAbsolutePath()) + "?clean", openInNewWindow, false);
return crudService.renderView(state);
} catch (JSONException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
use of com.qcadoo.view.api.ViewDefinitionState in project qcadoo by qcadoo.
the class ExportToPdfController method generatePdf.
@Monitorable(threshold = 500)
@ResponseBody
@RequestMapping(value = { L_CONTROLLER_PATH }, method = RequestMethod.POST)
public Object generatePdf(@PathVariable(L_PLUGIN_IDENTIFIER_VARIABLE) final String pluginIdentifier, @PathVariable(L_VIEW_NAME_VARIABLE) final String viewName, @RequestBody final JSONObject body, final Locale locale, @RequestHeader("User-Agent") final String userAgent) {
try {
changeMaxResults(body);
ViewDefinitionState state = crudService.invokeEvent(pluginIdentifier, viewName, body, locale);
GridComponent grid = (GridComponent) state.getComponentByReference(QcadooViewConstants.L_GRID);
if (grid == null) {
JSONArray args = body.getJSONObject("event").getJSONArray("args");
if (args.length() > 0) {
grid = (GridComponent) state.getComponentByReference(args.getString(0));
}
}
Document document = new Document(PageSize.A4.rotate());
String date = DateFormat.getDateInstance().format(new Date());
File file = fileService.createExportFile("export_" + grid.getName() + "_" + date + ".pdf");
FileOutputStream fileOutputStream = new FileOutputStream(file);
PdfWriter pdfWriter = PdfWriter.getInstance(document, fileOutputStream);
pdfWriter.setPageEvent(new PdfPageNumbering(footerResolver.resolveFooter(locale)));
document.setMargins(40, 40, 60, 60);
document.addTitle("export.pdf");
pdfHelper.addMetaData(document);
pdfWriter.createXmpMetadata();
document.open();
String title = translationService.translate(pluginIdentifier + "." + viewName + ".window.mainTab." + grid.getName() + ".header", locale);
Date generationDate = new Date();
pdfHelper.addDocumentHeader(document, "", title, translationService.translate("qcadooReport.commons.generatedBy.label", locale), generationDate);
List<String> columns = getColumns(grid);
List<String> columnNames = getColumnNames(grid, columns);
PdfPTable pdfTable = pdfHelper.createTableWithHeader(columnNames.size(), columnNames, false);
List<Map<String, String>> rows;
if (grid.getSelectedEntitiesIds().isEmpty()) {
rows = grid.getColumnValuesOfAllRecords();
} else {
rows = grid.getColumnValuesOfSelectedRecords();
}
addPdfTableCells(pdfTable, rows, columns, viewName);
document.add(pdfTable);
document.close();
boolean openInNewWindow = !StringUtils.isNoneBlank(userAgent) || (!userAgent.contains("Chrome") && !userAgent.contains("Safari")) || userAgent.contains("Edge");
state.redirectTo(fileService.getUrl(file.getAbsolutePath()) + "?clean", openInNewWindow, false);
return crudService.renderView(state);
} catch (JSONException | FileNotFoundException | DocumentException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
use of com.qcadoo.view.api.ViewDefinitionState in project qcadoo by qcadoo.
the class GridComponentStateTest method init.
@Before
public void init() throws Exception {
JSONObject jsonContent = new JSONObject();
jsonContent.put(GridComponentState.JSON_SELECTED_ENTITY_ID, 13L);
jsonContent.put(GridComponentState.JSON_MULTISELECT_MODE, false);
JSONObject jsonSelected = new JSONObject();
jsonSelected.put("13", true);
jsonContent.put(GridComponentState.JSON_SELECTED_ENTITIES, jsonSelected);
jsonContent.put(GridComponentState.JSON_BELONGS_TO_ENTITY_ID, 1L);
jsonContent.put(GridComponentState.JSON_FIRST_ENTITY, 60);
jsonContent.put(GridComponentState.JSON_MAX_ENTITIES, 30);
jsonContent.put(GridComponentState.JSON_FILTERS_ENABLED, true);
JSONArray jsonOrder = new JSONArray();
jsonOrder.put(ImmutableMap.of("column", "asd", "direction", "asc"));
jsonContent.put(GridComponentState.JSON_ORDER, jsonOrder);
JSONObject jsonFilters = new JSONObject();
jsonFilters.put("asd", "test");
jsonFilters.put("qwe", "test2");
jsonContent.put(GridComponentState.JSON_FILTERS, jsonFilters);
json = new JSONObject(Collections.singletonMap(AbstractComponentState.JSON_CONTENT, jsonContent));
Entity entity = mock(Entity.class);
given(entity.getField("name")).willReturn("text");
viewDefinitionState = mock(ViewDefinitionState.class);
productDataDefinition = mock(DataDefinition.class, RETURNS_DEEP_STUBS);
substituteDataDefinition = mock(DataDefinition.class, "substituteDataDefinition");
HasManyType substitutesFieldType = mock(HasManyType.class);
given(substitutesFieldType.getDataDefinition()).willReturn(substituteDataDefinition);
given(substitutesFieldType.getJoinFieldName()).willReturn("product");
substitutesFieldDefinition = mock(FieldDefinition.class);
given(substitutesFieldDefinition.getType()).willReturn(substitutesFieldType);
given(substitutesFieldDefinition.getName()).willReturn("substitutes");
given(substitutesFieldDefinition.getDataDefinition()).willReturn(substituteDataDefinition);
substituteCriteria = mock(SearchCriteriaBuilder.class);
given(substituteDataDefinition.getPluginIdentifier()).willReturn("plugin");
given(substituteDataDefinition.getName()).willReturn("substitute");
given(substituteDataDefinition.find()).willReturn(substituteCriteria);
given(productDataDefinition.getPluginIdentifier()).willReturn("plugin");
given(productDataDefinition.getName()).willReturn("product");
given(productDataDefinition.getField("substitutes")).willReturn(substitutesFieldDefinition);
columns = new LinkedHashMap<String, GridComponentColumn>();
TranslationService translationService = mock(TranslationService.class);
given(translationService.translate(Mockito.anyString(), Mockito.any(Locale.class))).willReturn("i18n");
given(translationService.translate(Mockito.anyString(), Mockito.anyString(), Mockito.any(Locale.class))).willReturn("i18n");
given(translationService.translate(Mockito.anyString(), Mockito.any(Locale.class))).willReturn("i18n");
GridComponentPattern pattern = mock(GridComponentPattern.class);
given(pattern.getColumns()).willReturn(columns);
given(pattern.getBelongsToFieldDefinition()).willReturn(substitutesFieldDefinition);
given(pattern.isActivable()).willReturn(false);
given(pattern.isWeakRelation()).willReturn(false);
ApplicationContext applicationContext = mock(ApplicationContext.class);
setField(pattern, "applicationContext", applicationContext);
SecurityRolesService securityRolesService = mock(SecurityRolesService.class);
given(applicationContext.getBean(SecurityRolesService.class)).willReturn(securityRolesService);
grid = new GridComponentState(productDataDefinition, pattern);
grid.setDataDefinition(substituteDataDefinition);
grid.setTranslationService(translationService);
new ExpressionServiceImpl().init();
}
Aggregations