use of com.manydesigns.portofino.resourceactions.m2m.configuration.SelectionProviderReference in project Portofino by ManyDesigns.
the class ConfigurationForm method writeTo.
public void writeTo(ManyToManyConfiguration m2mConfiguration) {
m2mConfiguration.setViewType(viewType);
m2mConfiguration.setDatabase(database);
m2mConfiguration.setQuery(query);
m2mConfiguration.setOneExpression(oneExpression);
m2mConfiguration.setOnePropertyName(onePropertyName);
if (StringUtils.isEmpty(oneSpName)) {
m2mConfiguration.setOneSelectionProvider(null);
} else {
boolean found = false;
for (ForeignKey fk : m2mConfiguration.getActualRelationTable().getForeignKeys()) {
if (fk.getName().equals(oneSpName)) {
SelectionProviderReference ref = new SelectionProviderReference();
ref.setForeignKeyName(oneSpName);
m2mConfiguration.setOneSelectionProvider(ref);
found = true;
break;
}
}
if (!found) {
for (ModelSelectionProvider sp : m2mConfiguration.getActualRelationTable().getSelectionProviders()) {
if (sp.getName().equals(oneSpName)) {
SelectionProviderReference ref = new SelectionProviderReference();
ref.setSelectionProviderName(oneSpName);
m2mConfiguration.setOneSelectionProvider(ref);
break;
}
}
}
}
if (StringUtils.isEmpty(manySpName) || m2mConfiguration.getActualRelationTable() == null) {
m2mConfiguration.setManySelectionProvider(null);
} else {
boolean found = false;
for (ForeignKey fk : m2mConfiguration.getActualRelationTable().getForeignKeys()) {
if (fk.getName().equals(manySpName)) {
SelectionProviderReference ref = new SelectionProviderReference();
ref.setForeignKeyName(manySpName);
m2mConfiguration.setManySelectionProvider(ref);
found = true;
break;
}
}
if (!found) {
for (ModelSelectionProvider sp : m2mConfiguration.getActualRelationTable().getSelectionProviders()) {
if (sp.getName().equals(manySpName)) {
SelectionProviderReference ref = new SelectionProviderReference();
ref.setSelectionProviderName(manySpName);
m2mConfiguration.setManySelectionProvider(ref);
break;
}
}
}
}
}
use of com.manydesigns.portofino.resourceactions.m2m.configuration.SelectionProviderReference in project Portofino by ManyDesigns.
the class ManyToManyAction method init.
@Override
public Object init() {
preparePage();
if (m2mConfiguration == null || m2mConfiguration.getActualRelationTable() == null || m2mConfiguration.getActualManyTable() == null) {
logger.error("Configuration is null or relation/many table not found (check previous log messages)");
// TODO WebApplicationException instead?
return this;
}
Table table = m2mConfiguration.getActualRelationTable();
relationTableAccessor = new TableAccessor(table);
manyTableAccessor = new TableAccessor(m2mConfiguration.getActualManyTable());
if (StringUtils.isBlank(m2mConfiguration.getActualOnePropertyName())) {
logger.error("One property name not set");
return this;
}
String expression = m2mConfiguration.getOneExpression();
if (!StringUtils.isBlank(expression)) {
// Set primary key
OgnlContext ognlContext = ElementsThreadLocals.getOgnlContext();
// TODO handle exception
onePk = OgnlUtils.getValueQuietly(expression, ognlContext, this);
correctlyConfigured = true;
} else {
assert !StringUtils.isBlank(m2mConfiguration.getActualOnePropertyName());
// Setup "one" selection
SelectionProviderReference oneSelectionProvider = m2mConfiguration.getOneSelectionProvider();
if (oneSelectionProvider != null) {
ModelSelectionProvider actualSelectionProvider = oneSelectionProvider.getActualSelectionProvider();
if (!(actualSelectionProvider instanceof DatabaseSelectionProvider)) {
logger.warn("Selection provider {} not supported", actualSelectionProvider);
return this;
}
TableAccessor tableAccessor = new TableAccessor(m2mConfiguration.getActualRelationTable());
PropertyAccessor onePkAccessor = null;
try {
onePkAccessor = tableAccessor.getProperty(m2mConfiguration.getActualOnePropertyName());
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
if (onePkAccessor == null) {
logger.warn("Not a property: {}", m2mConfiguration.getActualOnePropertyName());
return this;
}
String databaseName = m2mConfiguration.getActualOneDatabase().getDatabaseName();
DatabaseSelectionProvider sp = (DatabaseSelectionProvider) actualSelectionProvider;
DefaultSelectionProvider selectionProvider;
String name = sp.getName();
String hql = sp.getHql();
if (StringUtils.isNotEmpty(hql)) {
selectionProvider = createSelectionProviderFromHql(name, databaseName, hql, DisplayMode.DROPDOWN, SearchDisplayMode.DROPDOWN);
if (sp instanceof ForeignKey) {
selectionProvider.sortByLabel();
}
} else {
logger.warn("ModelSelection provider '{}': unsupported query", name);
return this;
}
Object myInstance = tableAccessor.newInstance();
oneSelectField = new SelectField(onePkAccessor, selectionProvider, Mode.EDIT, "__");
oneSelectField.setRequired(false);
oneSelectField.readFromRequest(context.getRequest());
oneSelectField.writeToObject(myInstance);
onePk = onePkAccessor.get(myInstance);
correctlyConfigured = true;
}
}
return this;
}
use of com.manydesigns.portofino.resourceactions.m2m.configuration.SelectionProviderReference in project Portofino by ManyDesigns.
the class ManyToManyAction method loadAssociations.
protected void loadAssociations() throws NoSuchFieldException {
Table table = m2mConfiguration.getActualRelationTable();
TableCriteria criteria = new TableCriteria(table);
// TODO chiave multipla
String onePropertyName = m2mConfiguration.getActualOnePropertyName();
PropertyAccessor onePropertyAccessor = relationTableAccessor.getProperty(onePropertyName);
// TODO chiave multipla
SelectionProviderReference manySelectionProvider = m2mConfiguration.getManySelectionProvider();
String manyPropertyName = manySelectionProvider.getActualSelectionProvider().getReferences().get(0).getActualFromColumn().getActualPropertyName();
PropertyAccessor manyPropertyAccessor = relationTableAccessor.getProperty(manyPropertyName);
criteria = criteria.eq(onePropertyAccessor, onePk);
QueryStringWithParameters queryString;
try {
queryString = QueryUtils.mergeQuery(m2mConfiguration.getQuery(), criteria, this);
} catch (RuntimeException e) {
RequestMessages.addErrorMessage("Invalid query");
throw e;
}
existingAssociations = QueryUtils.runHqlQuery(session, queryString.getQueryString(), queryString.getParameters());
availableAssociations = new ArrayList<Object>();
String databaseName = ((DatabaseSelectionProvider) manySelectionProvider.getActualSelectionProvider()).getToDatabase();
String hql = ((DatabaseSelectionProvider) manySelectionProvider.getActualSelectionProvider()).getHql();
if (!StringUtils.isEmpty(hql)) {
Session selectionProviderSession = persistence.getSession(databaseName);
QueryStringWithParameters manyQuery = QueryUtils.mergeQuery(hql, null, this);
potentiallyAvailableAssociations = QueryUtils.runHqlQuery(selectionProviderSession, manyQuery.getQueryString(), manyQuery.getParameters());
} else {
throw new RuntimeException("Couldn't determine many query");
}
PropertyAccessor[] manyKeyProperties = manyTableAccessor.getKeyProperties();
// TODO handle manyKeyProperties.length > 1
PropertyAccessor manyPkAccessor = manyTableAccessor.getProperty(manyKeyProperties[0].getName());
for (Object o : potentiallyAvailableAssociations) {
Object oPk = manyPkAccessor.get(o);
boolean existing = isExistingAssociation(manyPropertyAccessor, oPk);
if (!existing) {
availableAssociations.add(o);
}
}
}
use of com.manydesigns.portofino.resourceactions.m2m.configuration.SelectionProviderReference in project Portofino by ManyDesigns.
the class ManyToManyAction method addSpRefSelectionProvider.
protected void addSpRefSelectionProvider(final List<ModelSelectionProvider> sps, Form form, String fieldName) throws NoSuchFieldException {
SelectionProvider sp = SelectionProviderLogic.createSelectionProvider(fieldName, sps, ModelSelectionProvider.class, null, new String[] { "name" });
JavaClassAccessor acc = JavaClassAccessor.getClassAccessor(ManyToManyConfiguration.class);
form.get(0).add(new SelectField(acc.getProperty(fieldName), sp, Mode.EDIT, "") {
@Override
public void readFromRequest(HttpServletRequest req) {
String stringValue = req.getParameter(inputName);
if (!StringUtils.isEmpty(stringValue)) {
for (ModelSelectionProvider msp : sps) {
if (msp.getName().equals(stringValue)) {
SelectionProviderReference ref = new SelectionProviderReference();
if (msp instanceof ForeignKey) {
ref.setForeignKeyName(msp.getName());
} else {
ref.setSelectionProviderName(msp.getName());
}
selectionModel.setValue(selectionModelIndex, ref);
}
}
} else {
selectionModel.setValue(selectionModelIndex, null);
}
}
});
}
Aggregations