use of com.haulmont.cuba.core.entity.FileDescriptor in project rtcab-ordermanagement by mariodavid.
the class OrderEdit method generateInvoice.
public void generateInvoice() {
Report invoiceReport = reportLoadService.loadReportBySystemcode("order_invoice");
String fileName = "myFile";
Map<String, Object> params = new HashMap<>();
params.put("entity", getItem());
FileDescriptor invoiceFile = reportService.createAndSaveReport(invoiceReport, params, fileName);
Document invoiceDocument = metadata.create(Document.class);
invoiceDocument.setFile(invoiceFile);
invoiceDocument.setName(fileName);
invoiceDocument.setType(DocumentType.INVOICE);
invoiceDocument.setOrder(getItem());
documentsDs.addItem(invoiceDocument);
}
use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class EntityListenerTest method testEntityManager.
@Test
public void testEntityManager() throws Exception {
Server server;
UUID serverId;
Transaction tx = cont.persistence().createTransaction();
try {
// create
server = new Server();
server.setName("server1");
serverId = server.getId();
cont.persistence().getEntityManager().persist(server);
tx.commitRetaining();
assertNotNull(server.getData());
UUID relatedId = UUID.fromString(server.getData());
FileDescriptor related = cont.persistence().getEntityManager().find(FileDescriptor.class, relatedId);
assertNotNull(related);
assertEquals("Related", related.getName());
tx.commitRetaining();
// update
server = cont.persistence().getEntityManager().find(Server.class, serverId);
assertNotNull(server);
server.setName("server1 updated");
tx.commitRetaining();
related = cont.persistence().getEntityManager().find(FileDescriptor.class, relatedId);
assertNotNull(related);
assertEquals("Related updated", related.getName());
tx.commitRetaining();
// remove
server = cont.persistence().getEntityManager().find(Server.class, serverId);
assertNotNull(server);
cont.persistence().getEntityManager().remove(server);
tx.commitRetaining();
related = cont.persistence().getEntityManager().find(FileDescriptor.class, relatedId);
assertNull(related);
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class FileStorage method findOrphanDescriptors.
@Override
public String findOrphanDescriptors() {
com.haulmont.cuba.core.app.filestorage.FileStorage fileStorage;
FileStorageAPI fileStorageAPI = AppBeans.get(FileStorageAPI.class);
if (fileStorageAPI instanceof com.haulmont.cuba.core.app.filestorage.FileStorage) {
fileStorage = (com.haulmont.cuba.core.app.filestorage.FileStorage) fileStorageAPI;
} else {
return "<not supported>";
}
File[] roots = getStorageRoots();
if (roots.length == 0)
return "No storage directories defined";
StringBuilder sb = new StringBuilder();
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
TypedQuery<FileDescriptor> query = em.createQuery("select fd from sys$FileDescriptor fd", FileDescriptor.class);
List<FileDescriptor> fileDescriptors = query.getResultList();
for (FileDescriptor fileDescriptor : fileDescriptors) {
File dir = fileStorage.getStorageDir(roots[0], fileDescriptor);
File file = new File(dir, com.haulmont.cuba.core.app.filestorage.FileStorage.getFileName(fileDescriptor));
if (!file.exists()) {
sb.append(fileDescriptor.getId()).append(", ").append(fileDescriptor.getName()).append(", ").append(fileDescriptor.getCreateDate()).append("\n");
}
}
tx.commit();
} catch (Exception e) {
return ExceptionUtils.getStackTrace(e);
} finally {
tx.end();
}
return sb.toString();
}
use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class FileStorage method findOrphanFiles.
@Override
public String findOrphanFiles() {
FileStorageAPI fileStorageAPI = AppBeans.get(FileStorageAPI.class);
if (!(fileStorageAPI instanceof com.haulmont.cuba.core.app.filestorage.FileStorage)) {
return "<not supported>";
}
File[] roots = getStorageRoots();
if (roots.length == 0)
return "No storage directories defined";
StringBuilder sb = new StringBuilder();
File storageFolder = roots[0];
if (!storageFolder.exists())
return ExceptionUtils.getStackTrace(new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, storageFolder.getAbsolutePath()));
Collection<File> systemFiles = FileUtils.listFiles(storageFolder, null, true);
Collection<File> filesInRootFolder = FileUtils.listFiles(storageFolder, null, false);
// remove files of root storage folder (e.g. storage.log) from files collection
systemFiles.removeAll(filesInRootFolder);
List<FileDescriptor> fileDescriptors;
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
TypedQuery<FileDescriptor> query = em.createQuery("select fd from sys$FileDescriptor fd", FileDescriptor.class);
fileDescriptors = query.getResultList();
tx.commit();
} catch (Exception e) {
return ExceptionUtils.getStackTrace(e);
} finally {
tx.end();
}
Set<String> descriptorsFileNames = new HashSet<>();
for (FileDescriptor fileDescriptor : fileDescriptors) {
descriptorsFileNames.add(com.haulmont.cuba.core.app.filestorage.FileStorage.getFileName(fileDescriptor));
}
for (File file : systemFiles) {
if (!descriptorsFileNames.contains(file.getName()))
// Encode file path if it contains non-ASCII characters
if (!file.getPath().matches("\\p{ASCII}+")) {
String encodedFilePath = URLEncodeUtils.encodeUtf8(file.getPath());
sb.append(encodedFilePath).append("\n");
} else {
sb.append(file.getPath()).append("\n");
}
}
return sb.toString();
}
use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class DesktopFileUploadField method setDatasource.
@Override
public void setDatasource(Datasource datasource, String property) {
this.datasource = datasource;
if (datasource == null) {
setValue(null);
return;
}
MetaClass metaClass = datasource.getMetaClass();
resolveMetaPropertyPath(metaClass, property);
itemChangeListener = e -> {
if (updatingInstance)
return;
FileDescriptor descriptor = InstanceUtils.getValueEx(e.getItem(), metaPropertyPath.getPath());
updateComponent(descriptor);
fireChangeListeners(descriptor);
};
// noinspection unchecked
datasource.addItemChangeListener(new WeakItemChangeListener(datasource, itemChangeListener));
itemPropertyChangeListener = e -> {
if (updatingInstance)
return;
if (e.getProperty().equals(metaPropertyPath.toString())) {
updateComponent((FileDescriptor) e.getValue());
fireChangeListeners(e.getValue());
}
};
// noinspection unchecked
datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
initRequired(metaPropertyPath);
if ((datasource.getState() == Datasource.State.VALID) && (datasource.getItem() != null)) {
Object newValue = InstanceUtils.getValueEx(datasource.getItem(), metaPropertyPath.getPath());
FileDescriptor fileDescriptor = (FileDescriptor) newValue;
updateComponent(fileDescriptor);
fireChangeListeners(newValue);
}
if (metaProperty.isReadOnly()) {
setEditable(false);
}
handleFilteredAttributes(this, this.datasource, metaPropertyPath);
securityItemChangeListener = e -> handleFilteredAttributes(this, this.datasource, metaPropertyPath);
// noinspection unchecked
this.datasource.addItemChangeListener(new WeakItemChangeListener(this.datasource, securityItemChangeListener));
initBeanValidator();
}
Aggregations