Skip to content

Commit ca43c01

Browse files
authored
Merge pull request #99 from dotCMS/issue-contenttype
Adding an example about how to create a content type
2 parents 96f5fa0 + ba40408 commit ca43c01

File tree

3 files changed

+233
-0
lines changed

3 files changed

+233
-0
lines changed

com.dotcms.contenttype/README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# README
2+
3+
This bundle plugin is an example of how to add dotcms Content Type
4+
5+
## How to build this example
6+
7+
To build the JAR, run the following Maven command:
8+
```sh
9+
mvn clean install
10+
```
11+
12+
This will generate the plugin JAR in the `target` directory.
13+
14+
## How to install this bundle
15+
16+
* **To install this bundle:**
17+
18+
Copy the bundle JAR file inside the Felix OSGI container (`dotCMS/felix/load`).
19+
20+
OR
21+
22+
Upload the bundle JAR file using the dotCMS UI (`CMS Admin -> Plugins -> Upload Plugin`).
23+
24+
* **To uninstall this bundle:**
25+
26+
Remove the bundle JAR file from the Felix OSGI container (`dotCMS/felix/load`).
27+
28+
OR
29+
30+
Undeploy the bundle JAR using the dotCMS UI (`CMS Admin -> Plugins -> Undeploy`).
31+
32+
## How to create a Actionlet OSGi plugin
33+
34+
In order to create this OSGI plugin, Maven is configured to generate the `META-INF/MANIFEST.MF` file automatically. If needed, you can customize the configuration in the `pom.xml`.
35+
36+
Below is a description of the required fields in the `MANIFEST.MF` and how they are configured in a `pom.xml`:
37+
38+
> **Bundle-Name:** The name of your bundle
39+
> **Bundle-SymbolicName:** A short and unique name for the bundle
40+
> **Bundle-Vendor:** The vendor of the bundle (example: dotCMS)
41+
> **Bundle-Description:** A brief description of the bundle
42+
> **Bundle-DocURL:** URL for the bundle documentation
43+
> **Bundle-Activator:** Package and name of your Activator class (example: com.dotmarketing.osgi.actionlet.Activator)
44+
> **Export-Package:** Declares the packages that are visible outside the plugin. Any package not declared here has visibility only within the bundle.
45+
> **Import-Package:** This is a comma-separated list of the names of packages to import. This list must include the packages that you are using inside your OSGI bundle plugin and are exported and exposed by the dotCMS runtime.
46+
47+
These fields are configured in the `pom.xml` as follows:
48+
49+
```xml
50+
<plugin>
51+
<groupId>org.apache.felix</groupId>
52+
<artifactId>maven-bundle-plugin</artifactId>
53+
<version>5.1.9</version>
54+
<extensions>true</extensions>
55+
<configuration>
56+
<instructions>
57+
<Bundle-Name>Your Bundle Name</Bundle-Name>
58+
<Bundle-SymbolicName>com.example.yourbundle</Bundle-SymbolicName>
59+
<Bundle-Vendor>dotCMS</Bundle-Vendor>
60+
<Bundle-Description>dotCMS - OSGI Actionlet example</Bundle-Description>
61+
<Bundle-DocURL>https://dotcms.com/</Bundle-DocURL>
62+
<Bundle-Activator>com.dotmarketing.osgi.actionlet.Activator</Bundle-Activator>
63+
<Export-Package>com.example.yourbundle.package</Export-Package>
64+
<Import-Package>*</Import-Package>
65+
</instructions>
66+
</configuration>
67+
</plugin>
68+
```
69+
70+
## Beware (!)
71+
72+
In order to work inside the Apache Felix OSGI runtime, the import and export directives must be bidirectional:
73+
74+
* **Exported Packages**
75+
76+
The dotCMS must declare the set of packages that will be available to the OSGI plugins by updating the file: `dotCMS/WEB-INF/felix/osgi-extra.conf`.
77+
This can also be configured using the dotCMS UI (`CMS Admin -> Plugins -> Exported Packages`).
78+
79+
Only after the exported packages are defined in this list, can a plugin import the packages to use them inside the OSGI bundle.
80+
81+
* **Fragment (Deprecated)**
82+
83+
Previously, a bundle fragment was used to make its contents available to other bundles by exporting 3rd party libraries from dotCMS. Fragments do not participate in the lifecycle of the bundle and therefore cannot have a Bundle-Activator. As this is no longer required, this section is deprecated.
84+
85+
---
86+
## Components
87+
88+
### com.dotmarketing.osgi.actionlet.MyActionlet
89+
90+
Implementation of a WorkFlowActionlet object.
91+
92+
### Activator
93+
94+
This bundle activator extends from com.dotmarketing.osgi.GenericBundleActivator and implements BundleActivator.start().
95+
This activator will allow you to register the Content Type object using the GenericBundleActivator.registerActionlet method

com.dotcms.contenttype/pom.xml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.dotcms</groupId>
7+
<artifactId>osgi-contenttype-example</artifactId>
8+
<version>0.2</version>
9+
<packaging>bundle</packaging>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<maven.compiler.source>11</maven.compiler.source>
14+
<maven.compiler.target>11</maven.compiler.target>
15+
<dotcms-core.version>24.05.13</dotcms-core.version>
16+
</properties>
17+
18+
<repositories>
19+
<repository>
20+
<id>dotcms-repo</id>
21+
<url>https://artifactory.dotcms.cloud/artifactory/libs-release</url>
22+
</repository>
23+
</repositories>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>com.dotcms</groupId>
28+
<artifactId>dotcms-core</artifactId>
29+
<version>${dotcms-core.version}</version>
30+
<scope>provided</scope>
31+
<exclusions>
32+
<exclusion>
33+
<groupId>com.dotcms.core.plugins</groupId>
34+
<artifactId>com.dotcms.tika-api</artifactId>
35+
</exclusion>
36+
<exclusion>
37+
<groupId>com.ettrema</groupId>
38+
<artifactId>milton-servlet</artifactId>
39+
</exclusion>
40+
</exclusions>
41+
</dependency>
42+
<!-- Add more dependencies as needed -->
43+
</dependencies>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.felix</groupId>
49+
<artifactId>maven-bundle-plugin</artifactId>
50+
<version>5.1.9</version>
51+
<extensions>true</extensions>
52+
<configuration>
53+
<instructions>
54+
<Bundle-Vendor>dotCMS</Bundle-Vendor>
55+
<Bundle-Description>dotCMS - OSGI Content Type example</Bundle-Description>
56+
<Bundle-DocURL>https://dotcms.com/</Bundle-DocURL>
57+
<Bundle-Activator>com.dotmarketing.osgi.actionlet.Activator</Bundle-Activator>
58+
<Import-Package>*</Import-Package>
59+
</instructions>
60+
</configuration>
61+
</plugin>
62+
</plugins>
63+
</build>
64+
</project>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.dotmarketing.osgi.actionlet;
2+
3+
import com.dotcms.contenttype.model.field.DataTypes;
4+
import com.dotcms.contenttype.model.field.Field;
5+
import com.dotcms.contenttype.model.field.ImmutableBinaryField;
6+
import com.dotcms.contenttype.model.field.ImmutableTextField;
7+
import com.dotcms.contenttype.model.type.ContentType;
8+
import com.dotcms.contenttype.model.type.ImmutableSimpleContentType;
9+
import com.dotcms.contenttype.model.type.SimpleContentType;
10+
import com.dotmarketing.business.APILocator;
11+
import com.dotmarketing.exception.DotDataException;
12+
import com.dotmarketing.exception.DotSecurityException;
13+
import com.dotmarketing.osgi.GenericBundleActivator;
14+
import com.dotmarketing.portlets.workflows.business.WorkflowAPI;
15+
import org.osgi.framework.BundleContext;
16+
17+
import java.util.ArrayList;
18+
import java.util.HashSet;
19+
import java.util.List;
20+
import java.util.Set;
21+
22+
public class Activator extends GenericBundleActivator {
23+
24+
private static final String VARIABLE_NAME = "MyContentType";
25+
26+
@Override
27+
public void start ( BundleContext bundleContext ) throws Exception {
28+
29+
//Initializing services...
30+
initializeServices( bundleContext );
31+
32+
//Registering the test Actionlet
33+
registerContentType();
34+
}
35+
36+
private void registerContentType() throws DotDataException, DotSecurityException {
37+
38+
final SimpleContentType simpleContentType = ImmutableSimpleContentType.builder()
39+
.name("My Content Type")
40+
.variable(VARIABLE_NAME)
41+
.build();
42+
43+
final List<Field> newFields = new ArrayList<>();
44+
final ImmutableBinaryField screenshotField = ImmutableBinaryField.builder().name("Screenshot").variable("screenshot").build();
45+
final ImmutableTextField titleField = ImmutableTextField.builder().name("title").variable("title").build();
46+
final ImmutableTextField urlField = ImmutableTextField.builder().name("url").variable("url").required(true).indexed(true).unique(false).build();
47+
final ImmutableTextField orderField = ImmutableTextField.builder().name("order").dataType(DataTypes.INTEGER).variable("order").indexed(true).build();
48+
49+
newFields.add(screenshotField);
50+
newFields.add(titleField);
51+
newFields.add(urlField);
52+
newFields.add(orderField);
53+
final ContentType savedContentType = APILocator.getContentTypeAPI(APILocator.systemUser())
54+
.save(simpleContentType, newFields, null); // saves the content type with the new fields
55+
56+
// rel the system workflow with the content type
57+
final Set<String> workflowIds = new HashSet<>();
58+
workflowIds.add(WorkflowAPI.SYSTEM_WORKFLOW_ID);
59+
60+
APILocator.getWorkflowAPI().saveSchemeIdsForContentType(savedContentType, workflowIds);
61+
}
62+
63+
public void stop(BundleContext context) throws Exception {
64+
65+
// Remove the content type
66+
final ContentType contentType = APILocator.getContentTypeAPI(APILocator.systemUser()).find(VARIABLE_NAME);
67+
if (null != contentType) {
68+
APILocator.getContentTypeAPI(APILocator.systemUser()).delete(contentType);
69+
}
70+
//Unregister all the bundle services
71+
unregisterServices(context);
72+
}
73+
74+
}

0 commit comments

Comments
 (0)