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.resources.app.action; |
35 |
|
|
36 |
|
import static info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum.*; |
37 |
|
|
38 |
|
import info.magnolia.context.Context; |
39 |
|
import info.magnolia.event.EventBus; |
40 |
|
import info.magnolia.i18nsystem.SimpleTranslator; |
41 |
|
import info.magnolia.jcr.util.NodeTypes; |
42 |
|
import info.magnolia.jcr.util.NodeUtil; |
43 |
|
import info.magnolia.resourceloader.Resource; |
44 |
|
import info.magnolia.resourceloader.ResourceOrigin; |
45 |
|
import info.magnolia.resourceloader.jcr.JcrResourceOrigin; |
46 |
|
import info.magnolia.resourceloader.layered.LayeredResource; |
47 |
|
import info.magnolia.resources.app.workbench.ResourcesContainer; |
48 |
|
import info.magnolia.ui.api.action.ActionExecutionException; |
49 |
|
import info.magnolia.ui.api.app.AppContext; |
50 |
|
import info.magnolia.ui.api.event.AdmincentralEventBus; |
51 |
|
import info.magnolia.ui.api.event.ContentChangedEvent; |
52 |
|
import info.magnolia.ui.api.location.LocationController; |
53 |
|
import info.magnolia.ui.api.overlay.AlertCallback; |
54 |
|
import info.magnolia.ui.contentapp.detail.DetailLocation; |
55 |
|
import info.magnolia.ui.contentapp.detail.DetailView.ViewType; |
56 |
|
|
57 |
|
import java.io.IOException; |
58 |
|
import java.io.Reader; |
59 |
|
|
60 |
|
import javax.inject.Named; |
61 |
|
import javax.jcr.Node; |
62 |
|
import javax.jcr.RepositoryException; |
63 |
|
import javax.jcr.Session; |
64 |
|
|
65 |
|
import org.apache.commons.io.IOUtils; |
66 |
|
|
67 |
|
import com.vaadin.data.Item; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@deprecated@link |
77 |
|
|
78 |
|
@Deprecated |
|
|
| 89.1% |
Uncovered Elements: 5 (46) |
Complexity: 10 |
Complexity Density: 0.27 |
|
79 |
|
public class HotfixResourceAction extends AbstractResourceAction<HotfixResourceActionDefinition> { |
80 |
|
|
81 |
|
private final Item resourceItem; |
82 |
|
private final Context context; |
83 |
|
private final SimpleTranslator i18n; |
84 |
|
private final AppContext appContext; |
85 |
|
private final LocationController locationController; |
86 |
|
private final ResourceOrigin origin; |
87 |
|
private final EventBus eventBus; |
88 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
89 |
3 |
public HotfixResourceAction(HotfixResourceActionDefinition definition, Item resourceItem, Context context, AppContext appContext, SimpleTranslator i18n, LocationController locationController, ResourceOrigin origin, @Named(AdmincentralEventBus.NAME) EventBus eventBus) {... |
90 |
3 |
super(definition, origin); |
91 |
3 |
this.resourceItem = resourceItem; |
92 |
3 |
this.context = context; |
93 |
3 |
this.i18n = i18n; |
94 |
3 |
this.appContext = appContext; |
95 |
3 |
this.locationController = locationController; |
96 |
3 |
this.origin = origin; |
97 |
3 |
this.eventBus = eventBus; |
98 |
|
} |
99 |
|
|
|
|
| 84% |
Uncovered Elements: 4 (25) |
Complexity: 4 |
Complexity Density: 0.19 |
|
100 |
3 |
@Override... |
101 |
|
public void execute() throws ActionExecutionException { |
102 |
3 |
Resource resourceToHotfix = fetchResourceToHotfix(); |
103 |
3 |
Resource parent = resourceToHotfix.getParent(); |
104 |
|
|
105 |
3 |
if (parent == null) { |
106 |
0 |
throw new ActionExecutionException(new IllegalStateException("Could not get parent of resource to hotfix.")); |
107 |
|
} |
108 |
|
|
109 |
3 |
try { |
110 |
3 |
Session jcrSession = context.getJCRSession(JcrResourceOrigin.RESOURCES_WORKSPACE); |
111 |
3 |
if (jcrSession.nodeExists(resourceToHotfix.getPath())) { |
112 |
1 |
String title = i18n.translate("resources.actions.hotfixResource.notification.alreadyExists.title"); |
113 |
1 |
String body = i18n.translate("resources.actions.hotfixResource.notification.alreadyExists.body", resourceToHotfix.getPath()); |
114 |
1 |
appContext.openAlert(WARNING, title, body, i18n.translate("button.ok"), new AlertCallback() { |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
115 |
0 |
@Override... |
116 |
|
public void onOk() { |
117 |
|
} |
118 |
|
}); |
119 |
1 |
return; |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
2 |
Node parentNode = NodeUtil.createPath(jcrSession.getRootNode(), parent.getPath(), NodeTypes.Folder.NAME); |
124 |
2 |
Node resourceNode = createResourceNode(resourceToHotfix, parentNode, resourceToHotfix.getName()); |
125 |
2 |
jcrSession.save(); |
126 |
|
|
127 |
|
|
128 |
2 |
String resourceNodePath = resourceNode.getPath(); |
129 |
2 |
DetailLocation detailLocation = new DetailLocation(appContext.getName(), HOTFIX_SUBAPP_NAME, ViewType.EDIT, resourceNodePath, null); |
130 |
2 |
locationController.goTo(detailLocation); |
131 |
|
|
132 |
2 |
eventBus.fireEvent(new ContentChangedEvent(resourceNodePath)); |
133 |
|
|
134 |
2 |
appContext.openNotification(INFO, true, i18n.translate("resources.actions.hotfixResource.notification.success")); |
135 |
|
|
136 |
|
} catch (Exception e) { |
137 |
0 |
appContext.openNotification(ERROR, false, i18n.translate("resources.actions.hotfixResource.notification.error")); |
138 |
0 |
throw new ActionExecutionException(e); |
139 |
|
} |
140 |
|
} |
141 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
142 |
3 |
private Resource fetchResourceToHotfix() {... |
143 |
3 |
String resourcePath = (String) resourceItem.getItemProperty(ResourcesContainer.RESOURCE_PATH).getValue(); |
144 |
3 |
return ((LayeredResource) origin.getByPath(resourcePath)).getFirst(); |
145 |
|
} |
146 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 0.5 |
|
147 |
2 |
private Node createResourceNode(Resource resourceToHotfix, Node parentNode, String resourceName) throws RepositoryException, IOException {... |
148 |
2 |
try (Reader reader = resourceToHotfix.openReader()) { |
149 |
2 |
String content = IOUtils.toString(reader); |
150 |
2 |
Node resourceNode = parentNode.addNode(resourceName, NodeTypes.Content.NAME); |
151 |
2 |
resourceNode.setProperty(JcrResourceOrigin.TEXT_PROPERTY, content); |
152 |
2 |
return resourceNode; |
153 |
|
} |
154 |
|
} |
155 |
|
} |