Ejemplo n.º 1
0
  @Test
  public void testAllowVersionWrite() {
    DocumentModel doc = session.createDocumentModel("/", "doc", "File");
    doc.setPropertyValue("icon", "icon1");
    doc = session.createDocument(doc);
    DocumentRef verRef = session.checkIn(doc.getRef(), null, null);

    // regular version cannot be written
    DocumentModel ver = session.getDocument(verRef);
    ver.setPropertyValue("icon", "icon2");
    try {
      session.saveDocument(ver);
      fail("Should not allow version write");
    } catch (PropertyException e) {
      assertTrue(e.getMessage(), e.getMessage().contains("Cannot set property on a version"));
    }

    // with proper option, it's allowed
    ver.setPropertyValue("icon", "icon3");
    ver.putContextData(CoreSession.ALLOW_VERSION_WRITE, Boolean.TRUE);
    session.saveDocument(ver);
    // refetch to check
    ver = session.getDocument(verRef);
    assertEquals("icon3", ver.getPropertyValue("icon"));
  }
Ejemplo n.º 2
0
 @Override
 public Class<?> getType(ELContext context, Object base, Object property) {
   Class<?> type = null;
   if (base instanceof DocumentModel) {
     try {
       type = super.getType(context, base, property);
     } catch (PropertyNotFoundException e) {
       type = DocumentPropertyContext.class;
       context.setPropertyResolved(true);
     }
   } else if (base instanceof DocumentPropertyContext || base instanceof Property) {
     type = Object.class;
     if (base instanceof DocumentPropertyContext) {
       DocumentPropertyContext ctx = (DocumentPropertyContext) base;
       try {
         Property docProperty = getDocumentProperty(ctx, property);
         if (docProperty.isContainer()) {
           Property subProperty = getDocumentProperty(docProperty, property);
           if (subProperty.isList()) {
             type = List.class;
           }
         } else if (docProperty instanceof ArrayProperty) {
           type = List.class;
         }
       } catch (PropertyException pe) {
         // avoid errors, return Object
         log.warn(pe.toString());
       }
     } else if (base instanceof Property) {
       try {
         Property docProperty = (Property) base;
         Property subProperty = getDocumentProperty(docProperty, property);
         if (subProperty.isList()) {
           type = List.class;
         }
       } catch (PropertyException pe) {
         try {
           // try property getters to resolve
           // doc.schema.field.type for instance
           type = super.getType(context, base, property);
         } catch (PropertyNotFoundException e) {
           // avoid errors, log original error and return Object
           log.warn(pe.toString());
         }
       }
     }
     context.setPropertyResolved(true);
   }
   return type;
 }
Ejemplo n.º 3
0
  @Override
  public Object getValue(ELContext context, Object base, Object property) {
    Object value = null;
    if (base instanceof DocumentModel) {
      try {
        // try document getters first to resolve doc.id for instance
        value = super.getValue(context, base, property);
      } catch (PropertyNotFoundException e) {
        value = new DocumentPropertyContext((DocumentModel) base, (String) property);
        context.setPropertyResolved(true);
      }
    } else if (base instanceof DocumentPropertyContext) {
      try {
        DocumentPropertyContext ctx = (DocumentPropertyContext) base;
        Property docProperty = getDocumentProperty(ctx, property);
        value = getDocumentPropertyValue(docProperty);
      } catch (PropertyException pe) {
        // avoid errors, return null
        log.warn(pe.toString());
      }
      context.setPropertyResolved(true);
    } else if (base instanceof Property) {
      try {
        Property docProperty = (Property) base;
        Property subProperty = getDocumentProperty(docProperty, property);
        value = getDocumentPropertyValue(subProperty);
      } catch (PropertyException pe) {
        try {
          // try property getters to resolve doc.schema.field.type
          // for instance
          value = super.getValue(context, base, property);
        } catch (PropertyNotFoundException e) {
          // avoid errors, log original error and return null
          log.warn(pe.toString());
        }
      }
      context.setPropertyResolved(true);
    }

    return value;
  }
Ejemplo n.º 4
0
 @Override
 public void setValue(ELContext context, Object base, Object property, Object value) {
   if (base instanceof DocumentModel) {
     try {
       super.setValue(context, base, property, value);
     } catch (PropertyNotFoundException e) {
       // nothing else to set on doc model
     }
   } else if (base instanceof DocumentPropertyContext) {
     DocumentPropertyContext ctx = (DocumentPropertyContext) base;
     value = FieldAdapterManager.getValueForStorage(value);
     try {
       ctx.doc.setPropertyValue(getDocumentPropertyName(ctx, property), (Serializable) value);
     } catch (PropertyException e) {
       // avoid errors here too
       log.warn(e.toString());
     }
     context.setPropertyResolved(true);
   } else if (base instanceof Property) {
     try {
       Property docProperty = (Property) base;
       Property subProperty = getDocumentProperty(docProperty, property);
       value = FieldAdapterManager.getValueForStorage(value);
       subProperty.setValue(value);
     } catch (PropertyException pe) {
       try {
         // try property setters to resolve doc.schema.field.type
         // for instance
         super.setValue(context, base, property, value);
       } catch (PropertyNotFoundException e) {
         // log original error and avoid errors here too
         log.warn(pe.toString());
       }
     }
     context.setPropertyResolved(true);
   }
 }