1+ /**
2+ * The contents of this file are subject to the license and copyright
3+ * detailed in the LICENSE and NOTICE files at the root of the source
4+ * tree and available online at
5+ *
6+ * http://www.dspace.org/license/
7+ */
8+ package org .dspace .app .rest ;
9+
10+ import static org .dspace .app .rest .utils .ContextUtil .obtainContext ;
11+
12+ import java .sql .SQLException ;
13+ import java .util .Objects ;
14+ import java .util .UUID ;
15+ import javax .servlet .http .HttpServletRequest ;
16+
17+ import org .apache .logging .log4j .Logger ;
18+ import org .dspace .app .rest .model .ClarinLicenseRest ;
19+ import org .dspace .app .rest .repository .ClarinLicenseRestRepository ;
20+ import org .dspace .authorize .AuthorizeException ;
21+ import org .dspace .core .Context ;
22+ import org .dspace .eperson .EPerson ;
23+ import org .dspace .eperson .service .EPersonService ;
24+ import org .springframework .beans .factory .annotation .Autowired ;
25+ import org .springframework .security .access .prepost .PreAuthorize ;
26+ import org .springframework .web .bind .annotation .RequestMapping ;
27+ import org .springframework .web .bind .annotation .RequestMethod ;
28+ import org .springframework .web .bind .annotation .RestController ;
29+
30+ /**
31+ * Controller for import licenses into database.
32+ * Endpoint: /api/licenses/import/{value}
33+ * This controller can:
34+ * - import labels in json format into database (POST /api/licenses/import/labels)
35+ * - import extended mapping in json format - create mapped dictionary (POST /api/licenses/import/extendedMapping)
36+ * - import licenses in json format into database (POST /api/licenses/import/licenses)
37+ *
38+ * @author Michaela Paurikova (michaela.paurikova at dataquest.sk)
39+ */
40+ @ RestController
41+ @ RequestMapping ("/api/clarin/import" )
42+ public class ClarinLicenseImportController {
43+ private static final Logger log = org .apache .logging .log4j .LogManager
44+ .getLogger (ClarinLicenseImportController .class );
45+
46+ @ Autowired
47+ private EPersonService ePersonService ;
48+ @ Autowired
49+ private ClarinLicenseRestRepository clarinLicenseRestRepository ;
50+
51+ @ PreAuthorize ("hasAuthority('ADMIN')" )
52+ @ RequestMapping (method = RequestMethod .POST , path = "/license" )
53+ public ClarinLicenseRest importLicense (HttpServletRequest request )
54+ throws AuthorizeException , SQLException {
55+ Context context = obtainContext (request );
56+ if (Objects .isNull (context )) {
57+ throw new RuntimeException ("Context is null!" );
58+ }
59+ //get param
60+ UUID epersonUUID = UUID .fromString (request .getParameter ("eperson" ));
61+ EPerson ePerson = ePersonService .find (context , epersonUUID );
62+
63+ //set current user to eperson and create license
64+ EPerson currUser = context .getCurrentUser ();
65+ context .setCurrentUser (ePerson );
66+ //turn off authorization
67+ context .turnOffAuthorisationSystem ();
68+ ClarinLicenseRest clarinLicenseRest = clarinLicenseRestRepository .createAndReturn ();
69+ context .restoreAuthSystemState ();
70+ //set back current use
71+ context .setCurrentUser (currUser );
72+
73+ context .complete ();
74+ return clarinLicenseRest ;
75+ }
76+
77+
78+ }
0 commit comments