View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2014 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.handler.commons.editcontext;
21  
22  import java.util.Collections;
23  import java.util.Map;
24  
25  import org.apache.sling.commons.json.JSONException;
26  import org.apache.sling.commons.json.io.JSONWriter;
27  import org.jetbrains.annotations.NotNull;
28  import org.osgi.annotation.versioning.ProviderType;
29  
30  import com.day.cq.wcm.api.components.DropTarget;
31  
32  /**
33   * {@link DropTarget} implementation.
34   */
35  @ProviderType
36  /*
37   * This depends on the (deprecated) Sling Commons JSON API, which is tied into AEM public API:
38   * https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/commons/JSONItem.html
39   */
40  @SuppressWarnings("deprecation")
41  public final class DropTargetImpl implements DropTarget {
42  
43    private final String name;
44    private final String id;
45    private final String propertyName;
46    private String[] groups = new String[0];
47    private String[] accept = new String[] {
48        "*"
49    };
50    private Map<String, String> parameters = Collections.emptyMap();
51  
52    /**
53     * @param name drop target name
54     * @param propertyName property name
55     */
56    public DropTargetImpl(@NotNull String name, @NotNull String propertyName) {
57      this.name = name;
58      this.id = CSS_CLASS_PREFIX + this.name;
59      this.propertyName = propertyName;
60    }
61  
62    /**
63     * Returns the name of this drop target
64     * @return drop target name
65     */
66    @Override
67    public String getName() {
68      return this.name;
69    }
70  
71    /**
72     * Returns the id for the drop target element. this is currently the same
73     * as the name, prefixed with the {@link #CSS_CLASS_PREFIX}
74     * @return drop target id
75     */
76    @Override
77    public String getId() {
78      return this.id;
79    }
80  
81    /**
82     * Returns the property name to use for this drop target.
83     * @return the property name.
84     */
85    @Override
86    public String getPropertyName() {
87      return this.propertyName;
88    }
89  
90    /**
91     * Returns the DD groups for this target
92     * @return the DD groups.
93     */
94    @Override
95    public String[] getGroups() {
96      return this.groups;
97    }
98  
99    /**
100    * @param values the DD groups.
101    * @return this
102    */
103   public DropTargetImpl setGroups(String @NotNull [] values) {
104     this.groups = values;
105     return this;
106   }
107 
108   /**
109    * Returns the mime-type accept patterns
110    * @return the accept patterns
111    */
112   @Override
113   public String[] getAccept() {
114     return this.accept;
115   }
116 
117   /**
118    * @param values the accept patterns
119    * @return this
120    */
121   public DropTargetImpl setAccept(String @NotNull [] values) {
122     this.accept = values;
123     return this;
124   }
125 
126   /**
127    * Returns a map of additional parameters to be set on the target node
128    * @return a map of additional parameters.
129    */
130   @Override
131   public Map<String, String> getParameters() {
132     return this.parameters;
133   }
134 
135   /**
136    * @param value a map of additional parameters.
137    * @return this
138    */
139   public DropTargetImpl setParameters(@NotNull Map<String, String> value) {
140     this.parameters = value;
141     return this;
142   }
143 
144   @Override
145   public void write(JSONWriter out) throws JSONException {
146     out.object();
147     out.key("id").value(this.name);
148     out.key("name").value(this.propertyName);
149     out.key("accept").array();
150     for (String a : this.accept) {
151       out.value(a);
152     }
153     out.endArray();
154     out.key("groups").array();
155     for (String group : this.groups) {
156       out.value(group);
157     }
158     out.endArray();
159     if (!this.parameters.isEmpty()) {
160       out.key("params");
161       out.object();
162       for (Map.Entry<String, String> e : this.parameters.entrySet()) {
163         out.key(e.getKey()).value(e.getValue());
164       }
165       out.endObject();
166     }
167     out.endObject();
168   }
169 
170 }