cancel
Showing results for 
Search instead for 
Did you mean: 

Custom service, using alfresco java api

frankb
Champ in-the-making
Champ in-the-making
Hello,

Im currently creating a custom service which returns the data as a nested list, which can be used in Sencha Touch.
I'm struggling to find out two things.

1: How can I find the "iconURL" and retrieve it?
serviceRegistry.getNodeService().getProperty(file.getNodeRef(), ApplicationModel.PROP_ICON)
returns null. When I run :
alfresco/service/api/node/workspace/SpacesStore/9d0f9136-ca36-49c3-8bdc-41020b465b3e
there is a attribute called "alf:icon", but I cant figure out the way to get access to these attributes with the java api.

2: How can I find the "downloadURL" of the files which i'm processing? I would like to provide a URL to a file, so that when users click on a file the download of the file will start.

How can I achieve these 2 things with the Java api?
There are some services which returns these attributes, but I cant figure out how to do it myself in my custom service.

Thanks in advance,
Frank
5 REPLIES 5

frankb
Champ in-the-making
Champ in-the-making
Anyone? any help would be much appreciated.

mdavid_cu
Champ in-the-making
Champ in-the-making
Hello frankb,
   This method was tested with Alfresco 3.0 labs but I guess that it is compatible with your current version, I hope this help
   
public String getDownloadURL(NodeRef nodeRef){
      DictionaryService dictionaryService = serviceRegistry.getDictionaryService();
      NodeService nodeService = serviceRegistry.getNodeService();
      boolean isDocument = dictionaryService.isSubClass(nodeService.getType(nodeRef), ContentModel.TYPE_CONTENT);
      
      if(!isDocument)
         throw new AlfrescoRuntimeException("The node is a cm:content subclass");
      
      return "/alfresco/service/api/node/content/" + nodeRef.getStoreRef().getProtocol() + "/"
      + nodeRef.getStoreRef().getProtocol() + "/" + nodeRef.getId();
   }
   
   public String getIconURL(NodeRef nodeRef, boolean small){
      DictionaryService dictionaryService = serviceRegistry.getDictionaryService();
      NodeService nodeService = serviceRegistry.getNodeService();
      boolean isDocument = dictionaryService.isSubClass(nodeService.getType(nodeRef), ContentModel.TYPE_CONTENT);
      if(isDocument){
         return FileTypeImageUtils.getFileTypeImage((String) nodeService.getProperty(nodeRef, ContentModel.PROP_NAME), small);
      } else { //the node is subtype of folder
         return "/images/icons/"   + nodeService.getProperty(nodeRef, ApplicationModel.PROP_ICON)   + (small ? "-16" : "") + ".gif";
      }
   } 

mdavid_cu
Champ in-the-making
Champ in-the-making
Hello frankb,
   
      The method FileTypeImageUtils.getFileTypeImage(String fileName, boolean small)
      has a dependency with de FacesContext instance, so you must call the REST service by instantiating the faces context,
      an Example:
       your service url must be: http://localhost:8080/alfresco/faces/service/api/node/workspace/SpacesStore/9d0f9136-ca36-49c3-8bdc-...

frankb
Champ in-the-making
Champ in-the-making
Thanks for your reply!!

But I dont quite understand the last bit, about the facescontext. Could you explain this a bit further?

Frank

mdavid_cu
Champ in-the-making
Champ in-the-making
Hello Frank,
    The method org.alfresco.repo.web.scripts.FileTypeImageUtils.getFileTypeImage(String fileName, boolean small) internally makes a call to this other static method in the same class:
 private static String getFileTypeImage(FacesContext fc, ServletContext sc, String name, FileTypeImageSize size)
    {
       String image = null;
       String defaultImage = null;
       switch (size)
       {
          case Small:
             defaultImage = DEFAULT_FILE_IMAGE16; break;
          case Medium:
             defaultImage = DEFAULT_FILE_IMAGE32; break;
          case Large:
             defaultImage = DEFAULT_FILE_IMAGE64; break;
       }
      
       int extIndex = name.lastIndexOf('.');
       if (extIndex != -1 && name.length() > extIndex + 1)
       {
          String ext = name.substring(extIndex + 1).toLowerCase();
          String key = ext + ' ' + size.toString();
         
          // found file extension for appropriate size image
          synchronized (s_fileExtensionMap)
          {
             image = s_fileExtensionMap.get(key);
             if (image == null)
             {
                // not found create for first time
                if (size != FileTypeImageSize.Large)
                {
                   image = (size == FileTypeImageSize.Small ? IMAGE_PREFIX16 : IMAGE_PREFIX32) +
                            ext + IMAGE_POSTFIX_GIF;
                }
                else
                {
                   image = IMAGE_PREFIX64 + ext + IMAGE_POSTFIX_PNG;
                }
               
                // does this image exist on the web-server?
                if ((fc != null && fc.getExternalContext().getResourceAsStream(image) != null) ||
                    (sc != null && sc.getResourceAsStream(image) != null))
                {
                   // found the image for this extension - save it for later
                   s_fileExtensionMap.put(key, image);
                }
                else if ((fc == null) && (sc == null))
                {
                   // we have neither FacesContext nor ServerContext so return the default image but don't cache it
                   image = defaultImage;
                }
                else
                {
                   // not found, save the default image for this extension instead
                   s_fileExtensionMap.put(key, defaultImage);
                   image = defaultImage;
                }
             }
          }
       }
      
       return (image != null ? image : defaultImage);
    }

You can see, the dependency of the FacesContext and ServletContext, which are helpful for searching the right .gif or .png for the node name, but if both contexts are null, you'll always get a defaultImage, cause the searching can't be executed. By default the facesContext is initialized with the URI: http://localhost:8080/alfresco/faces/…, so you can resolve the problem, by executing this URI