Skip to contentMethod: unmarshal(ComponentJaxb)
1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
7: * %%
8: * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: *
24: * *********************************************************************************************************************
25: * #L%
26: */
27: package it.tidalwave.northernwind.core.impl.io;
28:
29: import javax.annotation.Nonnull;
30: import javax.inject.Inject;
31: import java.io.IOException;
32: import java.io.InputStream;
33: import jakarta.xml.bind.JAXBElement;
34: import jakarta.xml.bind.JAXBException;
35: import jakarta.xml.bind.Unmarshaller;
36: import org.springframework.beans.factory.annotation.Configurable;
37: import it.tidalwave.util.Id;
38: import it.tidalwave.role.io.Unmarshallable;
39: import it.tidalwave.dci.annotation.DciRole;
40: import it.tidalwave.northernwind.core.model.ModelFactory;
41: import it.tidalwave.northernwind.frontend.ui.Layout;
42: import it.tidalwave.northernwind.core.impl.io.jaxb.ComponentJaxb;
43: import it.tidalwave.northernwind.core.impl.io.jaxb.ComponentsJaxb;
44: import lombok.ToString;
45: import lombok.extern.slf4j.Slf4j;
46:
47: /***********************************************************************************************************************
48: *
49: * @author Fabrizio Giudici
50: *
51: **********************************************************************************************************************/
52: @Configurable @DciRole(datumType = Layout.class) @ToString @Slf4j
53: public class LayoutJaxbUnmarshallable implements Unmarshallable
54: {
55: @Inject
56: private ModelFactory modelFactory;
57:
58: @Inject
59: private Unmarshaller unmarshaller;
60:
61: /*******************************************************************************************************************
62: *
63: *
64: ******************************************************************************************************************/
65: public LayoutJaxbUnmarshallable (@Nonnull final Layout layout)
66: {
67: }
68:
69: /*******************************************************************************************************************
70: *
71: * {@inheritDoc}
72: *
73: ******************************************************************************************************************/
74: @Override @Nonnull @SuppressWarnings("unchecked")
75: public Layout unmarshal (@Nonnull final InputStream is)
76: throws IOException
77: {
78: try
79: {
80: final var componentsJaxb = ((JAXBElement<ComponentsJaxb>)unmarshaller.unmarshal(is)).getValue();
81:
82: if (!"1.0".equals(componentsJaxb.getVersion()))
83: {
84: throw new IOException("Unexpected version: " + componentsJaxb.getVersion());
85: }
86:
87: return unmarshal(componentsJaxb.getComponent());
88: }
89: catch (JAXBException e)
90: {
91: throw new IOException("", e);
92: }
93: }
94:
95: /*******************************************************************************************************************
96: *
97: *
98: ******************************************************************************************************************/
99: @Nonnull
100: private Layout unmarshal (@Nonnull final ComponentJaxb componentJaxb)
101: {
102: var layout = modelFactory.createLayout().withId(new Id(componentJaxb.getId()))
103: .withType(componentJaxb.getType())
104: .build();
105:
106:• for (final var childComponentJaxb : componentJaxb.getComponent())
107: {
108: layout = layout.withChild(unmarshal(childComponentJaxb));
109: }
110:
111: return layout;
112: }
113: }