1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.ui.editor.converter;
35
36 import info.magnolia.jcr.RuntimeRepositoryException;
37 import info.magnolia.ui.datasource.jcr.JcrDatasource;
38
39 import javax.jcr.ItemNotFoundException;
40 import javax.jcr.RepositoryException;
41
42 import org.apache.commons.lang3.StringUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import com.machinezoo.noexception.Exceptions;
47 import com.vaadin.data.Converter;
48 import com.vaadin.data.Result;
49 import com.vaadin.data.ValueContext;
50
51
52
53
54 public class JcrPathToIdentifierConverter extends AbstractJcrConverter<String> {
55
56 private final Logger log = LoggerFactory.getLogger(JcrPathToIdentifierConverter.class);
57
58 public JcrPathToIdentifierConverter(JcrDatasource datasource) {
59 super(datasource);
60 }
61
62 @Override
63 public Result<String> convertToModel(String path, ValueContext context) {
64 return Result.of(() -> {
65 if (StringUtils.isBlank(path)) {
66 return null;
67 }
68 return Exceptions.wrap().get(() -> getNodeByPath(path).getIdentifier());
69 }, Throwable::getMessage);
70 }
71
72 @Override
73 public String convertToPresentation(String uuid, ValueContext context) {
74 if (StringUtils.isBlank(uuid)) {
75 return null;
76 }
77
78 try {
79 return getNodeByIdentifier(uuid).getPath();
80 } catch (ItemNotFoundException e) {
81 return new MissingNode(uuid, Exceptions.wrap().get(this::getWorkspace)).getPath();
82 } catch (RepositoryException e) {
83 throw new RuntimeRepositoryException(e);
84 }
85 }
86 }