Пример #1
0
 public static TemplateHashModel useStaticPackage(String packageName) {
   try {
     BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
     TemplateHashModel staticModels = wrapper.getStaticModels();
     TemplateHashModel fileStatics = (TemplateHashModel) staticModels.get(packageName);
     return fileStatics;
   } catch (Exception e) {
     e.printStackTrace();
   }
   return null;
 }
  @Override
  protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {

    // Fetching request params
    Map<String, String> templateArgs = req.getServiceMatch().getTemplateVars();
    String storeId = templateArgs.get("storeId");
    String storeProtocol = templateArgs.get("storeProtocol");
    String uuid = templateArgs.get("uuid");

    // Getting the Store ID on which the changes are requested
    Pair<Long, StoreRef> store = nodeDao.getStore(new StoreRef(storeProtocol, storeId));
    if (store == null) {
      throw new IllegalArgumentException(
          "Invalid store reference: " + storeProtocol + "://" + storeId);
    }

    Set<NodeEntity> nodes = new HashSet<NodeEntity>();

    NodeEntity node = indexingService.getNodeByUuid(store, uuid);
    if (node != null) {
      nodes.add(node);
    }

    // Render them out
    Map<String, Object> model = new HashMap<String, Object>(1, 1.0f);
    model.put("qnameDao", qnameDao);
    model.put("nsResolver", namespaceService);
    model.put("nodes", nodes);
    model.put("storeId", storeId);
    model.put("storeProtocol", storeProtocol);
    model.put("propertiesUrlTemplate", propertiesUrlTemplate);

    // This allows to call the static method QName.createQName from the FTL template
    try {
      BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
      TemplateHashModel staticModels = wrapper.getStaticModels();
      TemplateHashModel qnameStatics =
          (TemplateHashModel) staticModels.get("org.alfresco.service.namespace.QName");
      model.put("QName", qnameStatics);
    } catch (Exception e) {
      throw new AlfrescoRuntimeException(
          "Cannot add BeansWrapper for static QName.createQName method to be used from a Freemarker template",
          e);
    }

    logger.debug(String.format("Attaching %s nodes to the WebScript template", nodes.size()));

    return model;
  }
  @Override
  public void addStaticClassSupport(
      Map<String, Object> contextObjects, String variableName, Class<?> variableClass) {

    try {
      BeansWrapper beansWrapper = BeansWrapper.getDefaultInstance();

      TemplateHashModel templateHashModel = beansWrapper.getStaticModels();

      TemplateModel templateModel = templateHashModel.get(variableClass.getCanonicalName());

      contextObjects.put(variableName, templateModel);
    } catch (TemplateModelException e) {
      if (_log.isWarnEnabled()) {
        _log.warn("Variable " + variableName + " registration fail", e);
      }
    }
  }