View Javadoc
1   /**
2    * This file Copyright (c) 2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.dam.external.app.field;
35  
36  import info.magnolia.dam.api.AssetProvider;
37  import info.magnolia.dam.api.AssetProvider.AssetNotFoundException;
38  import info.magnolia.dam.api.AssetProviderRegistry;
39  import info.magnolia.dam.api.Item;
40  import info.magnolia.dam.api.ItemKey;
41  import info.magnolia.dam.api.PathAwareAssetProvider;
42  import info.magnolia.ui.form.field.converter.IdentifierToPathConverter;
43  
44  import java.util.Locale;
45  import java.util.regex.Pattern;
46  
47  import javax.inject.Inject;
48  
49  import org.apache.commons.lang.StringUtils;
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  /**
54   * Converter between {@link ItemKey} and path.
55   */
56  public class AssetCompositeIdKeyTranslator implements IdentifierToPathConverter {
57  
58      private static final Logger log = LoggerFactory.getLogger(AssetCompositeIdKeyTranslator.class);
59  
60      private static final Pattern PATH_PATTERN = Pattern.compile("[a-zA-Z0-9]+\\..+(/.+)*");
61  
62      private final AssetProviderRegistry assetProviderRegistry;
63  
64      @Inject
65      public AssetCompositeIdKeyTranslator(AssetProviderRegistry assetProviderRegistry) {
66          this.assetProviderRegistry = assetProviderRegistry;
67      }
68  
69      @Override
70      public String convertToModel(String path, Class<? extends String> targetType, Locale locale) throws ConversionException {
71          String res = StringUtils.EMPTY;
72          if (StringUtils.isBlank(path)) {
73              return res;
74          }
75          if (!isPath(path)) {
76              if (ItemKey.isValid(path)) { // on first item click path is itemKey
77                  return path;
78              }
79          }
80          try {
81              String providerId = StringUtils.substringBefore(path, ".");
82              String assetId = StringUtils.substringAfter(path, ".");
83              AssetProvider assetProvider = assetProviderRegistry.getProviderById(providerId);
84              if (!(assetProvider instanceof PathAwareAssetProvider)) {
85                  return path;
86              }
87              Item item = ((PathAwareAssetProvider) assetProvider).getItem(assetId);
88              res = item.getItemKey().asString();
89          } catch (PathAwareAssetProvider.PathNotFoundException e) {
90              log.error("Unable to convert Path to UUID", e);
91          }
92          return res;
93      }
94  
95      @Override
96      public String convertToPresentation(String itemKeyId, Class<? extends String> targetType, Locale locale) throws ConversionException {
97          String res = StringUtils.EMPTY;
98          if (StringUtils.isBlank(itemKeyId)) {
99              return res;
100         }
101         if (!ItemKey.isValid(itemKeyId)) {
102             return res;
103         }
104         if (isPath(itemKeyId)) {
105             return res;
106         }
107         try {
108             String providerId = ItemKey.from(itemKeyId).getProviderId();
109             AssetProvider assetProvider = assetProviderRegistry.getProviderById(providerId);
110             Item item = assetProvider.getItem(ItemKey.from(itemKeyId));
111             res = assetProvider.getIdentifier() + "." + item.getPath();
112         } catch (AssetNotFoundException e) {
113             log.error("Unable to convert UUID to Path", e);
114         }
115         return res;
116     }
117 
118     private boolean isPath(String id) {
119         return PATH_PATTERN.matcher(id).matches();
120     }
121 
122     @Override
123     public Class<String> getModelType() {
124         return String.class;
125     }
126 
127     @Override
128     public Class<String> getPresentationType() {
129         return String.class;
130     }
131 
132     @Override
133     public void setWorkspaceName(String workspaceName) {};
134 }