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; |
56 |
|
|
57 |
|
import java.io.IOException; |
58 |
|
import java.io.Reader; |
59 |
|
|
60 |
|
import javax.inject.Inject; |
61 |
|
import javax.inject.Named; |
62 |
|
import javax.jcr.Node; |
63 |
|
import javax.jcr.RepositoryException; |
64 |
|
import javax.jcr.Session; |
65 |
|
|
66 |
|
import org.apache.commons.io.IOUtils; |
67 |
|
|
68 |
|
import com.vaadin.data.Item; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
@value |
74 |
|
|
75 |
|
@value |
76 |
|
|
77 |
|
|
78 |
|
|
|
|
| 89.1% |
Uncovered Elements: 6 (55) |
Complexity: 12 |
Complexity Density: 0.28 |
|
79 |
|
public class EditResourceAction extends AbstractResourceAction<EditResourceActionDefinition> { |
80 |
|
|
81 |
|
private final Item resourceItem; |
82 |
|
private final Context context; |
83 |
|
private final AppContext appContext; |
84 |
|
private final SimpleTranslator i18n; |
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 |
@Inject... |
90 |
|
public EditResourceAction(EditResourceActionDefinition definition, Item resourceItem, Context context, AppContext appContext, SimpleTranslator i18n, LocationController locationController, ResourceOrigin origin, @Named(AdmincentralEventBus.NAME) EventBus eventBus) { |
91 |
3 |
super(definition, origin); |
92 |
3 |
this.resourceItem = resourceItem; |
93 |
3 |
this.context = context; |
94 |
3 |
this.appContext = appContext; |
95 |
3 |
this.i18n = i18n; |
96 |
3 |
this.locationController = locationController; |
97 |
3 |
this.origin = origin; |
98 |
3 |
this.eventBus = eventBus; |
99 |
|
} |
100 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
101 |
3 |
@Override... |
102 |
|
public void execute() throws ActionExecutionException { |
103 |
3 |
Resource resource = unwrapResource(resourceItem); |
104 |
3 |
boolean isResourcePresentInJcr = ((LayeredResource) resource).getFirst().getOrigin() instanceof JcrResourceOrigin; |
105 |
|
|
106 |
|
|
107 |
3 |
if (!isResourcePresentInJcr) { |
108 |
3 |
copyResourceToJcr(); |
109 |
|
} |
110 |
|
|
111 |
3 |
String path = resource.getPath(); |
112 |
3 |
String appName = getDefinition().getAppName(); |
113 |
|
|
114 |
3 |
DetailLocation location = new DetailLocation(appName, HOTFIX_SUBAPP_NAME, DetailView.ViewType.EDIT, path, null); |
115 |
3 |
locationController.goTo(location); |
116 |
|
} |
117 |
|
|
|
|
| 82.6% |
Uncovered Elements: 4 (23) |
Complexity: 4 |
Complexity Density: 0.21 |
|
118 |
3 |
private void copyResourceToJcr() throws ActionExecutionException {... |
119 |
3 |
Resource resourceToHotfix = fetchResourceToHotfix(); |
120 |
3 |
Resource parent = resourceToHotfix.getParent(); |
121 |
|
|
122 |
3 |
if (parent == null) { |
123 |
0 |
throw new ActionExecutionException(new IllegalStateException("Could not get parent of resource to hotfix.")); |
124 |
|
} |
125 |
|
|
126 |
3 |
try { |
127 |
3 |
Session jcrSession = context.getJCRSession(JcrResourceOrigin.RESOURCES_WORKSPACE); |
128 |
3 |
if (jcrSession.nodeExists(resourceToHotfix.getPath())) { |
129 |
1 |
String title = i18n.translate("resources.actions.hotfixResource.notification.alreadyExists.title"); |
130 |
1 |
String body = i18n.translate("resources.actions.hotfixResource.notification.alreadyExists.body", resourceToHotfix.getPath()); |
131 |
1 |
appContext.openAlert(WARNING, title, body, i18n.translate("button.ok"), new AlertCallback() { |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
132 |
0 |
@Override... |
133 |
|
public void onOk() { |
134 |
|
} |
135 |
|
}); |
136 |
1 |
return; |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
2 |
Node parentNode = NodeUtil.createPath(jcrSession.getRootNode(), parent.getPath(), NodeTypes.Folder.NAME); |
141 |
2 |
Node resourceNode = createResourceNode(resourceToHotfix, parentNode, resourceToHotfix.getName()); |
142 |
2 |
jcrSession.save(); |
143 |
|
|
144 |
2 |
String resourceNodePath = resourceNode.getPath(); |
145 |
2 |
eventBus.fireEvent(new ContentChangedEvent(resourceNodePath)); |
146 |
|
|
147 |
2 |
appContext.openNotification(INFO, true, i18n.translate("resources.actions.hotfixResource.notification.success")); |
148 |
|
|
149 |
|
} catch (Exception e) { |
150 |
0 |
appContext.openNotification(ERROR, false, i18n.translate("resources.actions.hotfixResource.notification.error")); |
151 |
0 |
throw new ActionExecutionException(e); |
152 |
|
} |
153 |
|
} |
154 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
155 |
3 |
private Resource fetchResourceToHotfix() {... |
156 |
3 |
String resourcePath = (String) resourceItem.getItemProperty(ResourcesContainer.RESOURCE_PATH).getValue(); |
157 |
3 |
return ((LayeredResource) origin.getByPath(resourcePath)).getFirst(); |
158 |
|
} |
159 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 0.5 |
|
160 |
2 |
private Node createResourceNode(Resource resourceToHotfix, Node parentNode, String resourceName) throws RepositoryException, IOException {... |
161 |
2 |
try (Reader reader = resourceToHotfix.openReader()) { |
162 |
2 |
String content = IOUtils.toString(reader); |
163 |
2 |
Node resourceNode = parentNode.addNode(resourceName, NodeTypes.Content.NAME); |
164 |
2 |
resourceNode.setProperty(JcrResourceOrigin.TEXT_PROPERTY, content); |
165 |
2 |
return resourceNode; |
166 |
|
} |
167 |
|
} |
168 |
|
} |