use of com.manydesigns.portofino.resourceactions.crud.configuration.CrudProperty in project Portofino by ManyDesigns.
the class CrudAction method saveConfiguration.
@Override
protected boolean saveConfiguration(Object configuration) {
CrudConfiguration crudConfiguration = (CrudConfiguration) configuration;
List<SelectionProviderReference> sps = new ArrayList<>(crudConfiguration.getSelectionProviders());
crudConfiguration.getSelectionProviders().clear();
crudConfiguration.persistence = persistence;
crudConfiguration.init();
sps.forEach(sp -> {
ForeignKey fk = DatabaseLogic.findForeignKeyByName(crudConfiguration.getActualTable(), sp.getSelectionProviderName());
if (fk != null) {
sp.setForeignKeyName(sp.getSelectionProviderName());
sp.setSelectionProviderName(null);
}
if (sp.getSelectionProviderName() != null || sp.getForeignKeyName() != null) {
crudConfiguration.getSelectionProviders().add(sp);
}
});
List<CrudProperty> existingProperties = this.crudConfiguration.getProperties();
List<CrudProperty> configuredProperties = crudConfiguration.getProperties();
List<CrudProperty> newProperties = configuredProperties.stream().map(p1 -> {
Optional<CrudProperty> maybeP2 = existingProperties.stream().filter(p2 -> p1.getName().equals(p2.getName())).findFirst();
CrudProperty p2 = maybeP2.orElse(new CrudProperty());
p2.setName(p1.getName());
p2.setEnabled(p1.isEnabled());
p2.setInsertable(p1.isInsertable());
p2.setInSummary(p1.isInSummary());
p2.setLabel(p1.getLabel());
p2.setSearchable(p1.isSearchable());
p2.setUpdatable(p1.isUpdatable());
return p2;
}).collect(Collectors.toList());
crudConfiguration.setProperties(newProperties);
return super.saveConfiguration(crudConfiguration);
}
use of com.manydesigns.portofino.resourceactions.crud.configuration.CrudProperty in project Portofino by ManyDesigns.
the class ModelSelectionProviderSupport method createSelectionProvider.
protected SelectionProvider createSelectionProvider(DatabaseSelectionProvider current, String[] fieldNames, Class[] fieldTypes, DisplayMode dm, SearchDisplayMode sdm, String newHref, String newText) {
DefaultSelectionProvider selectionProvider;
boolean anyActiveProperty = false;
for (String propertyName : fieldNames) {
CrudProperty crudProperty = findProperty(propertyName, crudAction.getCrudConfiguration().getProperties());
if (crudProperty != null && crudProperty.isEnabled()) {
anyActiveProperty = true;
break;
}
}
if (!anyActiveProperty) {
// Dummy
selectionProvider = SelectionProviderLogic.createSelectionProvider(current.getName(), new Class[0], Collections.emptyList());
} else {
selectionProvider = createSelectionProvider(current, fieldNames, fieldTypes, dm, sdm);
}
if (selectionProvider != null) {
if (newHref != null) {
OgnlTextFormat tf = new OgnlTextFormat(newHref);
newHref = tf.format(crudAction);
String contextPath = ElementsThreadLocals.getHttpServletRequest().getContextPath();
if (newHref.startsWith("/") && !newHref.startsWith(contextPath)) {
newHref = contextPath + newHref;
}
tf = new OgnlTextFormat(newText);
newText = tf.format(crudAction);
}
selectionProvider.setCreateNewValueHref(newHref);
selectionProvider.setCreateNewValueText(newText);
}
return selectionProvider;
}
use of com.manydesigns.portofino.resourceactions.crud.configuration.CrudProperty in project Portofino by ManyDesigns.
the class CrudActionTest method testBlobs.
public void testBlobs() throws Exception {
MutableHttpServletRequest req = new MutableHttpServletRequest();
ElementsThreadLocals.setMultipart(req);
req.getServletContext().setInitParameter("portofino.api.root", "http://fake");
req.makeMultipart();
Column column = DatabaseLogic.findColumnByName(persistence.getModel(), "jpetstore", "PUBLIC", "PRODUCT", "DESCN");
Annotation ann = new Annotation(column, FileBlob.class.getName());
column.getAnnotations().add(ann);
persistence.initModel();
CrudAction crudAction = new CrudAction() {
public void commitTransaction() {
super.commitTransaction();
session.beginTransaction();
}
@NotNull
@Override
protected ClassAccessor filterAccordingToPermissions(ClassAccessor classAccessor) {
// Let's ignore Shiro
return classAccessor;
}
@Override
protected String getUrlEncoding() {
return PortofinoProperties.URL_ENCODING_DEFAULT;
}
};
CrudConfiguration configuration = new CrudConfiguration();
configuration.setDatabase("jpetstore");
configuration.setQuery("from product");
String metaFilenamePattern = "blob-{0}.properties";
String dataFilenamePattern = "blob-{0}.data";
crudAction.blobManager = new HierarchicalBlobManager(new File(System.getProperty("java.io.tmpdir")), metaFilenamePattern, dataFilenamePattern);
CrudProperty property = new CrudProperty();
property.setName("productid");
property.setEnabled(true);
property.setInsertable(true);
property.setUpdatable(true);
configuration.getProperties().add(property);
property = new CrudProperty();
property.setName("category");
property.setEnabled(true);
property.setInsertable(true);
property.setUpdatable(true);
configuration.getProperties().add(property);
property = new CrudProperty();
property.setName("descn");
property.setEnabled(true);
property.setInsertable(true);
property.setUpdatable(true);
configuration.getProperties().add(property);
property = new CrudProperty();
property.setName("name");
property.setEnabled(true);
property.setInsertable(true);
property.setUpdatable(true);
ann = new Annotation(column, Required.class.getName());
ann.getProperties().add(new Property("value", "true"));
property.getAnnotations().add(ann);
configuration.getProperties().add(property);
configuration.persistence = persistence;
configuration.init();
ActionInstance actionInstance = new ActionInstance(null, null, new ActionDescriptor(), CrudAction.class);
actionInstance.setConfiguration(configuration);
actionInstance.getParameters().add("1");
ActionContext actionContext = new ActionContext();
actionContext.setRequest(req);
actionContext.setActionPath("");
actionContext.setServletContext(req.getServletContext());
req.setParameter("productid", "1");
Map category = (Map) persistence.getSession("jpetstore").createQuery("from category").list().get(0);
req.setParameter("category", (String) category.get("catid"));
crudAction.persistence = persistence;
crudAction.setContext(actionContext);
crudAction.setActionInstance(actionInstance);
crudAction.init();
crudAction.setupForm(Mode.CREATE);
Field descnField = crudAction.getForm().findFieldByPropertyName("descn");
assertNotNull(descnField);
assertTrue(descnField instanceof FileBlobField);
File tmpFile = File.createTempFile("blob", "blob");
DiskFileItem fileItem = new DiskFileItem("descn", "application/octet-stream", false, tmpFile.getName(), 0, tmpFile.getParentFile()) {
@Override
public void delete() {
// Do nothing as we want to reuse this
}
};
OutputStream os = fileItem.getOutputStream();
IOUtils.write("some test data", os, req.getCharacterEncoding());
req.addFileItem("descn", fileItem);
req.setParameter("descn_operation", AbstractBlobField.UPLOAD_MODIFY);
crudAction.httpPostMultipart();
assertFalse(crudAction.form.validate());
AbstractBlobField blobField = (AbstractBlobField) crudAction.form.findFieldByPropertyName("descn");
assertNotNull(blobField.getValue());
assertEquals(tmpFile.getName(), blobField.getValue().getFilename());
assertEquals(fileItem.getSize(), blobField.getValue().getSize());
try {
crudAction.getBlobManager().loadMetadata(new Blob(blobField.getValue().getCode()));
fail("The blob was saved despite validation failing");
} catch (Exception e) {
}
crudAction.object = null;
req.setParameter(blobField.getCodeInputName(), blobField.getValue().getCode());
req.setParameter("name", "name");
req.setParameter("productid", "1");
req.setParameter("category", "BIRDS");
crudAction.httpPostMultipart();
assertTrue(crudAction.form.validate());
blobField = (FileBlobField) crudAction.form.findFieldByPropertyName("descn");
assertNotNull(blobField.getValue());
// This is necessary because the crud might reload the form
crudAction.blobManager.loadMetadata(blobField.getValue());
assertEquals(tmpFile.getName(), blobField.getValue().getFilename());
assertEquals(fileItem.getSize(), blobField.getValue().getSize());
try {
crudAction.blobManager.loadMetadata(new Blob(blobField.getValue().getCode()));
} catch (IOException e) {
e.printStackTrace();
fail("The blob was not saved");
}
crudAction.httpPutMultipart();
assertTrue(crudAction.form.validate());
blobField = (FileBlobField) crudAction.form.findFieldByPropertyName("descn");
assertNotNull(blobField.getValue());
// This is necessary because the crud might reload the form
crudAction.blobManager.loadMetadata(blobField.getValue());
assertEquals(tmpFile.getName(), blobField.getValue().getFilename());
String oldBlobCode = blobField.getValue().getCode();
assertEquals(fileItem.getSize(), blobField.getValue().getSize());
req.setParameter("descn_operation", FileBlobField.UPLOAD_MODIFY);
req.setFileItem("descn", fileItem);
crudAction.httpPutMultipart();
assertTrue(crudAction.form.validate());
blobField = (FileBlobField) crudAction.form.findFieldByPropertyName("descn");
assertNotNull(blobField.getValue());
// This is necessary because the crud might reload the form
crudAction.blobManager.loadMetadata(blobField.getValue());
assertEquals(tmpFile.getName(), blobField.getValue().getFilename());
String newBlobCode = blobField.getValue().getCode();
assertNotEquals(oldBlobCode, newBlobCode);
crudAction.blobManager.loadMetadata(new Blob(newBlobCode));
try {
crudAction.blobManager.loadMetadata(new Blob(oldBlobCode));
fail("The blob " + oldBlobCode + " should have been deleted");
} catch (IOException e) {
// Ok
}
Session session = persistence.getSession("jpetstore");
session.flush();
Object id = ((Map) crudAction.object).get("productid");
int qres = session.createSQLQuery("update product set descn = 'illegal' where productid = :id").setParameter("id", id).executeUpdate();
assertEquals(1, qres);
session.flush();
session.getTransaction().commit();
session.clear();
session.beginTransaction();
// Force loading the object from the DB
crudAction.getParameters().add(id.toString());
crudAction.parametersAcquired();
crudAction.setupForm(Mode.VIEW);
crudAction.form.readFromObject(crudAction.object);
BlobUtils.loadBlobs(crudAction.form, crudAction.getBlobManager(), false);
blobField = (FileBlobField) crudAction.form.findFieldByPropertyName("descn");
assertNotNull(blobField.getValue());
assertNotNull(blobField.getBlobError());
assertNull(blobField.getValue().getFilename());
qres = session.createSQLQuery("update product set descn = :blobCode where productid = :id").setParameter("id", id).setParameter("blobCode", newBlobCode).executeUpdate();
assertEquals(1, qres);
session.flush();
session.getTransaction().commit();
session.clear();
session.beginTransaction();
// Force reload
crudAction.parametersAcquired();
crudAction.httpDelete(Collections.emptyList());
try {
crudAction.blobManager.loadMetadata(new Blob(newBlobCode));
fail("The blob " + newBlobCode + " should have been deleted");
} catch (IOException e) {
// Ok
}
}
use of com.manydesigns.portofino.resourceactions.crud.configuration.CrudProperty in project Portofino by ManyDesigns.
the class UpstairsAction method setupColumn.
protected int setupColumn(ConnectionProvider connectionProvider, Column column, CrudConfiguration configuration, int columnsInSummary, String linkToParentProperty, boolean isPassword) {
if (column.getActualJavaType() == null) {
logger.debug("Column without a javaType, skipping: {}", column.getQualifiedName());
return columnsInSummary;
}
Table table = column.getTable();
@SuppressWarnings({ "StringEquality" }) boolean enabled = !(linkToParentProperty != NO_LINK_TO_PARENT && column.getActualPropertyName().equals(linkToParentProperty)) && !isUnsupportedProperty(column);
boolean inPk = DatabaseLogic.isInPk(column);
boolean inFk = DatabaseLogic.isInFk(column);
boolean inSummary = enabled && (inPk || columnsInSummary < maxColumnsInSummary) && !isPassword;
boolean updatable = enabled && !column.isAutoincrement() && !inPk;
boolean insertable = enabled && !column.isAutoincrement();
if (!configuration.isLargeResultSet()) {
detectBooleanColumn(connectionProvider, table, column);
}
if (enabled && inPk && !inFk && Number.class.isAssignableFrom(column.getActualJavaType()) && !column.isAutoincrement()) {
for (PrimaryKeyColumn pkc : table.getPrimaryKey().getPrimaryKeyColumns()) {
if (pkc.getActualColumn().equals(column)) {
pkc.setGenerator(new IncrementGenerator(pkc));
insertable = false;
break;
}
}
}
if (isPassword) {
Annotation annotation = DatabaseLogic.findAnnotation(column, Password.class);
if (annotation == null) {
column.getAnnotations().add(new Annotation(column, Password.class.getName()));
}
insertable = false;
updatable = false;
}
if (!isPassword && column.getActualJavaType() == String.class && (column.getLength() == null || column.getLength() > MULTILINE_THRESHOLD)) {
Annotation annotation = DatabaseLogic.findAnnotation(column, Multiline.class);
if (annotation == null) {
annotation = new Annotation(column, Multiline.class.getName());
annotation.setProperty("value", "true");
column.getAnnotations().add(annotation);
}
}
CrudProperty crudProperty = new CrudProperty();
crudProperty.setEnabled(enabled);
crudProperty.setName(column.getActualPropertyName());
crudProperty.setInsertable(insertable);
crudProperty.setUpdatable(updatable);
if (inSummary) {
crudProperty.setInSummary(true);
crudProperty.setSearchable(true);
columnsInSummary++;
}
configuration.getProperties().add(crudProperty);
return columnsInSummary;
}
Aggregations