You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2018/01/24 15:16:44 UTC

[isis] 08/20: ISIS-1569: defines CommandWithDto as an optional interface for Command SPI implementations to have their Command's implement (translatable into a CommandDto) along with out-of-the-box ContentMapingService to convert these into CommandsDto structure.

This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch ISIS-1569-replay-commands
in repository https://gitbox.apache.org/repos/asf/isis.git

commit b48a171b68073ea20ef0011111bf060338ee5c0f
Author: Dan Haywood <da...@haywood-associates.co.uk>
AuthorDate: Thu Jan 18 10:33:07 2018 +0000

    ISIS-1569: defines CommandWithDto as an optional interface for Command SPI implementations to have their Command's implement (translatable into a CommandDto) along with out-of-the-box ContentMapingService to convert these into CommandsDto structure.
---
 .../conmap/ContentMappingServiceForCommandDto.java | 60 +++++++++++++++++
 .../ContentMappingServiceForCommandsDto.java       | 78 ++++++++++++++++++++++
 .../applib/services/command/CommandWithDto.java    | 26 ++++++++
 3 files changed, 164 insertions(+)

diff --git a/core/applib/src/main/java/org/apache/isis/applib/conmap/ContentMappingServiceForCommandDto.java b/core/applib/src/main/java/org/apache/isis/applib/conmap/ContentMappingServiceForCommandDto.java
new file mode 100644
index 0000000..0eb1235
--- /dev/null
+++ b/core/applib/src/main/java/org/apache/isis/applib/conmap/ContentMappingServiceForCommandDto.java
@@ -0,0 +1,60 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.isis.applib.conmap;
+
+import java.util.List;
+
+import javax.ws.rs.core.MediaType;
+
+import org.apache.isis.applib.annotation.DomainService;
+import org.apache.isis.applib.annotation.NatureOfService;
+import org.apache.isis.applib.annotation.Programmatic;
+import org.apache.isis.applib.services.command.CommandWithDto;
+import org.apache.isis.schema.cmd.v1.CommandDto;
+
+@DomainService(
+        nature = NatureOfService.DOMAIN,
+        menuOrder = "" + Integer.MAX_VALUE
+)
+public class ContentMappingServiceForCommandDto implements ContentMappingService {
+
+    @Programmatic
+    public Object map(Object object, final List<MediaType> acceptableMediaTypes) {
+        final boolean supported = Util.isSupported(CommandDto.class, acceptableMediaTypes);
+        if(!supported) {
+            return null;
+        }
+
+        return asDto(object);
+    }
+
+
+    static CommandDto asDto(final Object object) {
+        CommandDto commandDto = null;
+        if(object instanceof CommandWithDto) {
+            final CommandWithDto commandWithDto = (CommandWithDto) object;
+            commandDto = commandWithDto.asDto();
+        }
+        if(object instanceof CommandDto) {
+            commandDto = (CommandDto) object;
+        }
+        return commandDto;
+    }
+
+}
diff --git a/core/applib/src/main/java/org/apache/isis/applib/conmap/ContentMappingServiceForCommandsDto.java b/core/applib/src/main/java/org/apache/isis/applib/conmap/ContentMappingServiceForCommandsDto.java
new file mode 100644
index 0000000..4f886a9
--- /dev/null
+++ b/core/applib/src/main/java/org/apache/isis/applib/conmap/ContentMappingServiceForCommandsDto.java
@@ -0,0 +1,78 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.isis.applib.conmap;
+
+import java.util.List;
+
+import javax.ws.rs.core.MediaType;
+
+import org.apache.isis.applib.annotation.DomainService;
+import org.apache.isis.applib.annotation.NatureOfService;
+import org.apache.isis.applib.annotation.Programmatic;
+import org.apache.isis.schema.cmd.v1.CommandDto;
+import org.apache.isis.schema.cmd.v1.CommandsDto;
+
+@DomainService(
+        nature = NatureOfService.DOMAIN,
+        menuOrder = "" + Integer.MAX_VALUE
+)
+public class ContentMappingServiceForCommandsDto implements ContentMappingService {
+
+    @Programmatic
+    public Object map(Object object, final List<MediaType> acceptableMediaTypes) {
+        final boolean supported = Util.isSupported(CommandsDto.class, acceptableMediaTypes);
+        if(!supported) {
+            return null;
+        }
+
+        if(object instanceof CommandsDto) {
+            return object;
+        }
+
+        CommandDto commandDto = asDto(object);
+        if(commandDto != null) {
+            final CommandsDto commandsDto = new CommandsDto();
+            commandsDto.getCommandDto().add(commandDto);
+            return commandsDto;
+        }
+
+        if (object instanceof List) {
+            final List list = (List) object;
+            final CommandsDto commandsDto = new CommandsDto();
+            for (final Object o : list) {
+                final CommandDto objAsCommandDto = asDto(o);
+                if(objAsCommandDto != null) {
+                    commandsDto.getCommandDto().add(objAsCommandDto);
+                } else {
+                    // ignore entire list because found something that is not convertible.
+                    return null;
+                }
+            }
+            return commandsDto;
+        }
+
+        // else
+        return null;
+    }
+
+    private static CommandDto asDto(final Object object) {
+        return ContentMappingServiceForCommandDto.asDto(object);
+    }
+
+}
diff --git a/core/applib/src/main/java/org/apache/isis/applib/services/command/CommandWithDto.java b/core/applib/src/main/java/org/apache/isis/applib/services/command/CommandWithDto.java
new file mode 100644
index 0000000..e0e1b60
--- /dev/null
+++ b/core/applib/src/main/java/org/apache/isis/applib/services/command/CommandWithDto.java
@@ -0,0 +1,26 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.isis.applib.services.command;
+
+import org.apache.isis.applib.annotation.Programmatic;
+import org.apache.isis.schema.cmd.v1.CommandDto;
+
+public interface CommandWithDto extends Command {
+
+    @Programmatic
+    CommandDto asDto();
+}

-- 
To stop receiving notification emails like this one, please contact
danhaywood@apache.org.