public String getChangeSummary() { this.changeSummary = super.getString("CHANGE_SUMMARY"); return changeSummary; }
@Override public void convert(Model m) { Object val = m.get(attributeName); if (!(val instanceof java.util.Date) && !blank(val)) { try { long time = df.parse(val.toString()).getTime(); java.sql.Date d = new java.sql.Date(time); m.set(attributeName, d); } catch (ParseException e) { m.addValidator(this, attributeName); } } }
@Override public Long getId() { this.id = super.getLong("ID"); return id; }
@Override public void validate(Model m) { if(m.get(attribute) == null){ m.addValidator(this, attribute); return; } Object value = m.get(attribute); if (!(value instanceof String)) { throw new IllegalArgumentException("attribute " + attribute + " is not String"); } Matcher matcher = pattern.matcher((String) value); if(!matcher.matches()){ m.addValidator(this, attribute); } } }
fireBeforeUpdate(); doUpdatedAt(); List<Object> values = getAttributeValues(attributeNames); query.append(", "); query.append("updated_at = ?"); values.add(get("updated_at")); query.append(", "); query.append(metaModelLocal.getVersionColumn()).append(" = ?"); values.add(getLong(metaModelLocal.getVersionColumn()) + 1); if (getCompositeKeys() != null) { String[] compositeKeys = getCompositeKeys(); for (int i = 0; i < compositeKeys.length; i++) { query.append(i == 0 ? " WHERE " : " AND ").append(compositeKeys[i]).append(" = ?"); values.add(get(compositeKeys[i])); values.add(getId()); if(metaModel.hasPartitionIDs()){ for (String partitionId : metaModel.getPartitionIDs()) { query.append(" AND ").append(partitionId).append(" = ?"); values.add(get(partitionId)); values.add(get(metaModelLocal.getVersionColumn())); "', with " + getIdName() + " = " + getId() + " and " + metaModelLocal.getVersionColumn() + " = " + get(metaModelLocal.getVersionColumn()) +
String fkName; if (ass != null) { fkValue = get(ass.getFkName()); fkName = ass.getFkName(); } else if (assP != null) { fkValue = get("parent_id"); fkName = "parent_id"; if (!assP.getTypeLabel().equals(getString("parent_type"))) { throw new IllegalArgumentException("Wrong parent: '" + parentClass + "'. Actual parent type label of this record is: '" + getString("parent_type") + "'"); try { P parent = parentClass.newInstance(); parent.hydrate(results.get(0), true); if (parentMM.cached()) { QueryCache.instance().addItem(parentTable, query, new Object[]{fkValue}, parent); setCachedParent(parent);
private void processPolymorphicChildren(OneToManyPolymorphicAssociation association) { if (delegate.isEmpty()) {//no need to process children if no models selected. return; } MetaModel childMetaModel = metaModelOf(association.getTargetClass()); Map<Object, List<Model>> childrenByParentId = new HashMap<>(); List<Object> ids = collect(metaModel.getIdName()); StringBuilder query = new StringBuilder().append("parent_id IN ("); appendQuestions(query, ids.size()); query.append(") AND parent_type = '").append(association.getTypeLabel()).append('\''); for (Model child : new LazyList<>(query.toString(), childMetaModel, ids.toArray()).orderBy(childMetaModel.getIdName())) { if (childrenByParentId.get(child.get("parent_id")) == null) { childrenByParentId.put(child.get("parent_id"), new SuperLazyList<>()); } childrenByParentId.get(child.get("parent_id")).add(child); } for (T parent : delegate) { List<Model> children = childrenByParentId.get(parent.getId()); if (children != null) { parent.setChildren(childMetaModel.getModelClass(), children); } else { parent.setChildren(childMetaModel.getModelClass(), new SuperLazyList<>()); } } }
fireBeforeDelete(); int result; if (getCompositeKeys() != null) { String[] compositeKeys = getCompositeKeys(); StringBuilder query = new StringBuilder(); Object[] values = new Object[compositeKeys.length]; for (int i = 0; i < compositeKeys.length; i++) { query.append(i == 0 ? "DELETE FROM " + metaModelLocal.getTableName() + " WHERE " : " AND ").append(compositeKeys[i]).append(" = ?"); values[i] = get(compositeKeys[i]); StringBuilder query = new StringBuilder("DELETE FROM ").append(metaModelLocal.getTableName()).append(" WHERE ").append(getIdName()).append(" = ?"); List<Object> values = new ArrayList<>(); values.add(getId()); values.add(get(partitionId)); fireAfterDelete(); return true; fireAfterDelete(); return false;
/** * Verifies if the passed value for attributeName will set this instance to modified state. */ private boolean willAttributeModifyModel(String attributeName, Object newValue) { Object currentValue = get(attributeName); return currentValue != null ? !currentValue.equals(newValue) : newValue != null; }
private void processParent(BelongsToAssociation association) { if (delegate.isEmpty()) { // no need to process parents if no models selected. return; } //need to remove duplicates because more than one child can belong to the same parent. Set<Object> distinctParentIds = collectDistinct(association.getFkName()); distinctParentIds.remove(null); // remove null parent id if (distinctParentIds.isEmpty()) { return; } final MetaModel parentMetaModel = metaModelOf(association.getTargetClass()); final Map<Object, Model> parentById = new HashMap<>(); StringBuilder query = new StringBuilder().append(parentMetaModel.getIdName()).append(" IN ("); appendQuestions(query, distinctParentIds.size()); query.append(')'); for (Model parent : new LazyList<>(query.toString(), parentMetaModel, distinctParentIds.toArray())) { parentById.put(parent.getId(), parent); } //now that we have the parents in the has, we need to distribute them into list of children that are //stored in the delegate. for (Model child : delegate) { // parent could be null, which is fine child.setCachedParent(parentById.get(child.get(association.getFkName()))); } }
if (getId() != null) { child.set(fkName, getId()); child.saveIt();//this will cause an exception in case validations fail. }else if(metaModel.hasAssociation(child.getClass(), Many2ManyAssociation.class)){ Many2ManyAssociation ass = metaModel.getAssociationForTarget(child.getClass(), Many2ManyAssociation.class); if (child.getId() == null) { child.saveIt(); getId(), child.getId()); } else { joinModel.set(ass.getSourceFkName(), getId()); joinModel.set(ass.getTargetFkName(), child.getId()); joinModel.saveIt(); } catch (InstantiationException e) { throw new InitException("failed to create a new instance of class: " + joinMetaModel.getClass() child.set("parent_id", getId()); child.set("parent_type", ass.getTypeLabel()); child.saveIt();
MetaModel metaModel = getMetaModelLocal(); if (getId() != null) { child.set(fkName, getId()); child.saveIt();//this will cause an exception in case validations fail. }else if(metaModel.hasAssociation(childTable, Many2ManyAssociation.class)){ Many2ManyAssociation ass = metaModel.getAssociationForTarget(childTable, Many2ManyAssociation.class); if (child.getId() == null) { child.saveIt(); getId(), child.getId()); } else { joinModel.set(ass.getSourceFkName(), getId()); joinModel.set(ass.getTargetFkName(), child.getId()); joinModel.saveIt(); } catch (InstantiationException e) { throw new InitException("failed to create a new instance of class: " + joinMetaModel.getClass() OneToManyPolymorphicAssociation ass = metaModel.getAssociationForTarget( childTable, OneToManyPolymorphicAssociation.class); child.set("parent_id", getId()); child.set("parent_type", ass.getTypeLabel()); child.saveIt();
/** * Dumps content of list to a stream. Use for debugging. * @param out */ public void dump(OutputStream out){ hydrate(); PrintWriter p = new PrintWriter(out); for(Model m : delegate){ p.write(m.toString()); p.write('\n'); } p.flush(); }
private void validateMax(Double value, Model m){ if(value >= max){ m.addValidator(this, attribute); } }
public static <T extends Model> T createIt(Class<T> clazz, Object... namesAndValues) { T model = create(clazz, namesAndValues); model.saveIt(); return model; }
private void fireAfterCreate() { afterCreate(); for (CallbackListener callback : modelRegistryLocal().callbacks()) { callback.afterCreate(this); } }
private void fireAfterDelete() { afterDelete(); for (CallbackListener callback : modelRegistryLocal().callbacks()) { callback.afterDelete(this); } }
private void fireAfterSave() { afterSave(); for (CallbackListener callback : modelRegistryLocal().callbacks()) { callback.afterSave(this); } }
/** * This method will not exit silently like {@link #save()}, it instead will throw {@link org.javalite.activejdbc.validation.ValidationException} * if validations did not pass. * * @return true if the model was saved, false if you set an ID value for the model, but such ID does not exist in DB. */ public boolean saveIt() { boolean result = save(); ModelDelegate.purgeEdges(metaModelLocal); if (!errors.isEmpty()) { throw new ValidationException(this); } return result; }