@@ -21,52 +21,121 @@ To add `java-odoo-xml-rpc-core` to your project, add the following to your `pom.
2121
2222## Basic Usage
2323
24+ Here's a sample class with methods we used frequently from OdooClient:
25+
2426``` java
25- public static void main(String [] args) throws MalformedURLException , XmlRpcException {
26- // Initialize an OdooClient to fetch answers
27- OdooClient cli = new OdooClient (ODOO_URL , DBNAME , USERNAME , PASSWORD , true );
28- LOG . info(cli. getVersion()); // The version of your Odoo instance
29-
30- OdooId idToFetch = new OdooId ();
31- idToFetch. id = 2 ;
32- // Fetch a single Object by Odoo ID
33- Project project = cli. fetchObjectById(" project.project" , idToFetch, Project . class);
34- LOG . info(project. getDisplayName()); // The project's display name attribute
35-
36- OdooId id2fetch = new OdooId ();
37- id2fetch. id = 3 ;
38-
39- // Fetch multiple objects by Odoo ID
40- List<Project > projects = cli. fetchListByIds(" project.project" , Arrays . asList(idToFetch, id2fetch), Project . class);
41- LOG . info(projects. stream(). map(pt - > pt. getDisplayName()). collect(Collectors . joining(" ," )));
42-
43- // Fetch relationships for an odoo object, not recursively
44- cli. fetchRelationShips(project, Arrays . asList(ProjectTask . class, ResUsers . class));
45- System . out. println(" 1) " + cli. findByCriteria(1 , Project . class, " name" , " =" , " Sample Project" ). stream(). map(a - > a. getName()). collect(Collectors . joining(" ," )));
46- System . out. println(" 2) " + cli. findByCriteria(1 , Project . class, " name" , " like" , " %Sample%" ). stream(). map(a - > a. getName()). collect(Collectors . joining(" ," )));
47- System . out. println(" 3) " + cli. findByCriteria(1 , Project . class). stream(). map(a - > a. getName()). collect(Collectors . joining(" ," )));
48- System . out. println(" 4) " + cli. findByCriteria(1 , Project . class, " id" , " =" , " 1" ). stream(). map(a - > a. getName()). collect(Collectors . joining(" ," )));
49-
50- // Find a list of objects using search criteria, with a limit specified - the first parameter, here 1.
51- // If no criteria is specified then everything will be fetched.
52- List<TimesheetsAnalysisReport > timesheet = cli. findByCriteria(1 , TimesheetsAnalysisReport . class);
53- System . out. println(" 5) " + timesheet. stream(). map(a - > a. getName()). collect(Collectors . joining(" ," )));
54-
55- // If 0, then will fetch all objects.
56- List<TimesheetsAnalysisReport > ts = cli. findByCriteria(0 , TimesheetsAnalysisReport . class);
57- System . out. println(" 6) " + ts. stream(). map(a - > a. getName()). collect(Collectors . joining(" ," )));
58-
59- // Fetch recursively with depth = 2
60- cli. fetchRecursivelyRelationShips(ts. get(ts. size() - 1 ), 2 , Collections . emptyList());
61-
62- System . out. println(ts. get(ts. size() - 1 ). getCompanyIdAsObject());
63- System . out. println(ts. get(ts. size() - 1 ). getCompanyIdAsObject(). getName());
64- System . out. println(ts. get(ts. size() - 1 ). getCompanyIdAsObject(). getCurrencyIdAsObject(). getDisplayName());
65-
66- // Fetch using the criterion name like %Sample%
67- List<Project > sampleProjects = cli. findByCriteria(1 , Project . class, " name" , " like" , " %Sample%" );
68- // LOG.info("1) {}", sampleProjects.stream().map(a -> a.name).collect(Collectors.joining(",")));
69- cli. fetchRelationShips(sampleProjects. get(0 ), Collections . emptyList());
70- System . out. println(sampleProjects. get(0 ). getTasksAsList(). get(0 ));
27+ package ch.helvethink.odoo.sample ;
28+
29+ import ch.helvethink.odoo.models.project.Project ;
30+ import ch.helvethink.odoo.models.project.ProjectTask ;
31+ import ch.helvethink.odoo.models.res.ResCompany ;
32+ import ch.helvethink.odoo.models.res.ResPartner ;
33+ import ch.helvethink.odoo.models.res.ResUsers ;
34+ import ch.helvethink.odoo.models.timesheets.analysis.TimesheetsAnalysisReport ;
35+ import ch.helvethink.odoo4java.models.OdooId ;
36+ import ch.helvethink.odoo4java.xmlrpc.OdooClient ;
37+ import org.apache.xmlrpc.XmlRpcException ;
38+ import org.slf4j.Logger ;
39+ import org.slf4j.LoggerFactory ;
40+
41+ import java.net.MalformedURLException ;
42+ import java.util.Arrays ;
43+ import java.util.Collections ;
44+ import java.util.List ;
45+ import java.util.stream.Collectors ;
46+
47+ public class OdooClientSampleUsage {
48+
49+
50+ public static final String USERNAME = System . getenv(" username" );
51+ public static final String DBNAME = System . getenv(" dbname" );
52+ public static final String PASSWORD = System . getenv(" password" );
53+ public static final String ODOO_URL = System . getenv(" url" );
54+
55+ private static final Logger LOG = LoggerFactory . getLogger(OdooClientSampleUsage . class);
56+
57+ public static void main (String [] args ) throws MalformedURLException , XmlRpcException {
58+ // Initialize an OdooClient to fetch answers
59+
60+ OdooClient cli = new OdooClient (ODOO_URL , DBNAME , USERNAME , PASSWORD , true );
61+
62+ LOG . info(cli. getVersion());
63+
64+ OdooId idToFetch = new OdooId ();
65+ idToFetch. id = 2 ;
66+
67+ // Fetch a single Object by Odoo ID
68+ Project project = cli. fetchObjectById(" project.project" , idToFetch, Project . class);
69+ LOG . info(project. getDisplayName());
70+
71+ OdooId id2fetch = new OdooId ();
72+ id2fetch. id = 3 ;
73+ // Fetch multiple objects by Odoo IDs
74+ List<Project > projects = cli. fetchListByIds(" project.project" , Arrays . asList(idToFetch, id2fetch), Project . class);
75+ LOG . info(projects. stream(). map(pt - > pt. getDisplayName()). collect(Collectors . joining(" ," )));
76+
77+ // Fetch relationships for an odoo object, not recursively, filtering classes we want to fetch
78+
79+ cli. fetchRelationShips(project, Arrays . asList(ProjectTask . class, ResUsers . class));
80+
81+ LOG . info(" 1) Find by criteria 'equals' - {}" , cli. findByCriteria(1 , Project . class, " name" , " =" , " Sample Project" ). stream(). map(a - > a. getName()). collect(Collectors . joining(" ," )));
82+ LOG . info(" 2) Find by criteria 'like' - {}" , cli. findByCriteria(1 , Project . class, " name" , " like" , " %Sample%" ). stream(). map(a - > a. getName()). collect(Collectors . joining(" ," )));
83+ LOG . info(" 3) Find by criteria limit 1 without criterion - {}" , cli. findByCriteria(1 , Project . class). stream(). map(a - > a. getName()). collect(Collectors . joining(" ," )));
84+ LOG . info(" 4) Find by criteria id equals - {}" , cli. findByCriteria(1 , Project . class, " id" , " =" , " 1" ). stream(). map(a - > a. getName()). collect(Collectors . joining(" ," )));
85+
86+
87+ // Find a list of objects using search criteria, with a limit specified - the first parameter, here 1.
88+ // If no criteria is specified then everything will be fetched.
89+ List<TimesheetsAnalysisReport > timesheet = cli. findByCriteria(1 , TimesheetsAnalysisReport . class);
90+ LOG . info(" 5) Find by criteria limit 1 without criterion - {}" + timesheet. stream(). map(a - > a. getName()). collect(Collectors . joining(" ," )));
91+
92+ // If 0, then will fetch all objects.
93+ List<TimesheetsAnalysisReport > ts = cli. findByCriteria(0 , TimesheetsAnalysisReport . class);
94+ LOG . info(" 6) Find by criteria no limit without criterion - {}" + ts. stream(). map(a - > a. getName()). collect(Collectors . joining(" ," )));
95+
96+ // Fetch recursively with depth = 2
97+ cli. fetchRecursivelyRelationShips(ts. get(ts. size() - 1 ), 2 , Collections . emptyList());
98+ // Check that we fetched Currency Too
99+ LOG . info(ts. get(ts. size() - 1 ). getCompanyIdAsObject(). getName());
100+ LOG . info(ts. get(ts. size() - 1 ). getCompanyIdAsObject(). getCurrencyIdAsObject(). getDisplayName());
101+
102+ // Fetch using the criterion name like %Sample%
103+ List<Project > sampleProjects = cli. findByCriteria(1 , Project . class, " name" , " like" , " %Sample%" );
104+ cli. fetchRelationShips(sampleProjects. get(0 ), Collections . emptyList());
105+ LOG . info(sampleProjects. get(0 ). getTasksAsList(). get(0 ). getDisplayName());
106+ }
71107}
108+ ```
109+
110+ N.B: Model classes have been generated using the java-odoo-xml-rpc-plugin:
111+
112+ ``` xml
113+ <plugin >
114+ <groupId >ch.helvethink.odoo4java</groupId >
115+ <artifactId >java-odoo-xml-rpc-plugin</artifactId >
116+ <version >0.0.1-SNAPSHOT</version >
117+ <configuration >
118+ <generatedClassesRootPackage >ch.helvethink.odoo.models</generatedClassesRootPackage >
119+ <generatedClassPath >target/generated-sources</generatedClassPath >
120+ <odooDatabase >ANGRY_MARMOT_ODOO_DB</odooDatabase >
121+ <odooUsername >ANGRY_MARMOT_USERNAME</odooUsername >
122+ <odooInstanceUrl >https://ANGRY_MARMOT_ODOO_URL</odooInstanceUrl >
123+ <odooPassword >ANGRY_MARMOT_PASS</odooPassword >
124+ <includedPrefixes >
125+ <include >account</include >
126+ <include >project</include >
127+ <include >res</include >
128+ <include >timesheets</include >
129+ </includedPrefixes >
130+ </configuration >
131+ <executions >
132+ <execution >
133+ <id >generate-sources</id >
134+ <goals >
135+ <goal >generate</goal >
136+ </goals >
137+ <phase >generate-sources</phase >
138+ </execution >
139+ </executions >
140+ </plugin >
72141```
0 commit comments