@@ -3,12 +3,39 @@ var yeoman = require('yeoman-generator');
33var chalk = require ( 'chalk' ) ;
44var packagejs = require ( __dirname + '/../../package.json' ) ;
55
6+ var path = require ( 'path' ) ,
7+ util = require ( 'util' ) ,
8+ _ = require ( 'lodash' ) ,
9+ _s = require ( 'underscore.string' ) ,
10+ fs = require ( 'fs' ) ,
11+ glob = require ( "glob" ) ;
12+
613// Stores JHipster variables
714var jhipsterVar = { moduleName : 'postgresuuid-converter' } ;
815
916// Stores JHipster functions
1017var jhipsterFunc = { } ;
1118
19+ var longToUUID = function ( file ) {
20+ jhipsterFunc . replaceContent ( file , 'Long' , 'UUID' , true ) ;
21+ } ;
22+
23+ // TODO: Handle other types in entity properly. Not with brute force.
24+ var convertIDtoUUIDwithCol = function ( file , importNeedle , columnName ) {
25+ jhipsterFunc . replaceContent ( file , importNeedle , importNeedle + '\nimport java.util.UUID;\nimport org.hibernate.annotations.GenericGenerator;' ) ;
26+ jhipsterFunc . replaceContent ( file , 'strategy = GenerationType.AUTO' , 'generator = "UUIDGenerator"' ) ;
27+ jhipsterFunc . replaceContent ( file , '@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")' , '@GeneratedValue(strategy = GenerationType.AUTO, generator = "UUIDGenerator")' ) ;
28+ jhipsterFunc . replaceContent ( file , '@SequenceGenerator(name = "sequenceGenerator")' , '@GenericGenerator(name = "UUIDGenerator", strategy = "uuid2")' ) ;
29+ jhipsterFunc . replaceContent ( file , '@Id' , '@Id\n @Column(name = "' + columnName + '", columnDefinition = "uuid")\n @org.hibernate.annotations.Type(type="pg-uuid")' ) ;
30+ longToUUID ( file ) ;
31+ //jhipsterFunc.replaceContent(file, 'public Long getId()', 'public UUID getId()');
32+ //jhipsterFunc.replaceContent(file, 'public void setId(Long id)', 'public void setId(UUID id)');
33+ } ;
34+
35+ var convertIDtoUUID = function ( file , importNeedle ) {
36+ convertIDtoUUIDwithCol ( file , importNeedle , 'id' ) ;
37+ } ;
38+
1239module . exports = yeoman . Base . extend ( {
1340
1441 initializing : {
@@ -25,47 +52,249 @@ module.exports = yeoman.Base.extend({
2552 } ,
2653 displayLogo : function ( ) {
2754 // Have Yeoman greet the user.
28- this . log ( 'Welcome to the ' + chalk . red ( 'JHipster postgresuuid-converter' ) + ' generator! ' + chalk . yellow ( 'v' + packagejs . version + '\n' ) ) ;
55+ this . log ( 'Welcome to the ' + chalk . red ( 'JHipster Aquevix Postgres to UUID Converter' ) + ' generator! ' + chalk . yellow ( 'v' + packagejs . version + '\n' ) ) ;
56+ // this.log('Variables:');
57+ // this.log(JSON.stringify(jhipsterVar));
58+ } ,
59+ checkDBType : function ( ) {
60+ if ( jhipsterVar . databaseType != 'sql' && jhipsterVar . prodDatabaseType != 'postgresql' ) {
61+ // exit if DB type is not Postgres
62+ this . abort = true ;
63+ }
64+ } ,
65+ getEntitityNames : function ( ) {
66+ var existingEntities = [ ] ,
67+ existingEntityChoices = [ ] ,
68+ existingEntityNames = [ ] ;
69+ try {
70+ if ( fs . existsSync ( '.jhipster' ) ) {
71+ existingEntityNames = fs . readdirSync ( '.jhipster' ) ;
72+ } else {
73+ this . log ( chalk . yellow . bold ( 'WARN' ) + ' Could not read entities, you might not have generated any entities yet. I will continue to update core files, entities will not be updated...\n' ) ;
74+ }
75+ } catch ( e ) {
76+ this . log ( e ) ;
77+ this . log ( chalk . red . bold ( 'ERROR!' ) + ' Could not read entities folder, you might not have generated any entities yet. I will continue to update core files, entities will not be updated...\n' ) ;
78+ }
79+
80+ existingEntityNames . forEach ( function ( entry ) {
81+ if ( entry . indexOf ( '.json' ) !== - 1 ) {
82+ var entityName = entry . replace ( '.json' , '' ) ;
83+ existingEntities . push ( entityName ) ;
84+ existingEntityChoices . push ( { name : entityName , value : entityName } ) ;
85+ }
86+ } ) ;
87+ this . existingEntities = existingEntities ;
88+ this . existingEntityChoices = existingEntityChoices ;
89+ this . log ( JSON . stringify ( existingEntities ) ) ;
2990 }
3091 } ,
3192
32- prompting : function ( ) {
33- var done = this . async ( ) ;
93+ /*
94+ prompting: function () {
95+ var done = this.async();
3496
35- var prompts = [ {
36- type : 'input' ,
37- name : 'message' ,
38- message : 'Please put something' ,
39- default : 'hello world!'
40- } ] ;
97+ //var prompts = [
98+ // {
99+ // type: 'confirm',
100+ // name: 'convertUUID',
101+ // message: 'Do you want to convert to UUID?',
102+ // default: true
103+ // }
104+ //];
41105
42- this . prompt ( prompts , function ( props ) {
43- this . props = props ;
44- // To access props later use this.props.someOption;
45-
46- done ( ) ;
47- } . bind ( this ) ) ;
48- } ,
106+ this.prompt(prompts, function (props) {
107+ this.props = props;
108+ // To access props later use this.props.someOption;
49109
110+ done();
111+ }.bind(this));
112+ },
113+ */
50114 writing : {
51115 writeTemplates : function ( ) {
116+ } ,
117+ setupGlobalVar : function ( ) {
52118 this . baseName = jhipsterVar . baseName ;
53119 this . packageName = jhipsterVar . packageName ;
120+ this . packageFolder = jhipsterVar . packageFolder ;
54121 this . angularAppName = jhipsterVar . angularAppName ;
55- var javaDir = jhipsterVar . javaDir ;
56- var resourceDir = jhipsterVar . resourceDir ;
57- var webappDir = jhipsterVar . webappDir ;
122+ this . frontendBuilder = jhipsterVar . frontendBuilder ;
123+ this . buildTool = jhipsterVar . buildTool ;
124+ this . databaseType = jhipsterVar . databaseType ;
125+ this . changelogDate = jhipsterFunc . dateFormatForLiquibase ( ) ;
126+ this . webappDir = jhipsterVar . webappDir ;
127+ this . javaTemplateDir = 'src/main/java/package' ;
128+ this . javaDir = jhipsterVar . javaDir ;
129+ this . resourceDir = jhipsterVar . resourceDir ;
130+ this . interpolateRegex = / < % = ( [ \s \S ] + ?) % > / g; // so that thymeleaf tags in templates do not get mistreated as _ templates
131+ this . copyFiles = function ( files ) {
132+ files . forEach ( function ( file ) {
133+ jhipsterFunc . copyTemplate ( file . from , file . to , file . type ? file . type : TPL , this , file . interpolate ? { 'interpolate' : file . interpolate } : undefined ) ;
134+ } , this ) ;
135+ } ;
136+ } ,
137+
138+ updateBaseFiles : function ( ) {
139+
140+ var uuidGeneratorAnnotation = '@GeneratedValue.*"UUIDGenerator"' ;
141+ var pattern = new RegExp ( uuidGeneratorAnnotation , 'g' ) ;
142+ var content = this . fs . read ( this . javaDir + 'domain/User.java' , 'utf8' ) ;
143+ if ( ! pattern . test ( content ) )
144+ {
145+ convertIDtoUUID ( this . javaDir + 'domain/User.java' , 'import java.time.ZonedDateTime;' ) ;
146+
147+ jhipsterFunc . replaceContent ( this . javaDir + 'domain/PersistentAuditEvent.java' , ' @Column(name = "event_id")\n' , '' ) ;
148+ convertIDtoUUIDwithCol ( this . javaDir + 'domain/PersistentAuditEvent.java' , 'import java.util.Map;' , 'event_id' ) ;
149+
150+ // And the Repository
151+ jhipsterFunc . replaceContent ( this . javaDir + 'repository/UserRepository.java' , 'import java.util.List;' , 'import java.util.List;\nimport java.util.UUID;' ) ;
152+ longToUUID ( this . javaDir + 'repository/UserRepository.java' ) ;
153+ // jhipsterFunc.replaceContent(this.javaDir + 'repository/UserRepository.java', 'JpaRepository<User, Long>', 'JpaRepository<User, UUID>');
154+ // jhipsterFunc.replaceContent(this.javaDir + 'repository/UserRepository.java', 'findOneById(Long userId)', 'findOneById(UUID userId)');
155+
156+ jhipsterFunc . replaceContent ( this . javaDir + 'repository/PersistenceAuditEventRepository.java' , 'import java.util.List;' , 'import java.util.List;\nimport java.util.UUID;' ) ;
157+ longToUUID ( this . javaDir + 'repository/PersistenceAuditEventRepository.java' ) ;
158+ //jhipsterFunc.replaceContent(this.javaDir + 'repository/PersistenceAuditEventRepository.java', 'JpaRepository<PersistentAuditEvent, Long>', 'JpaRepository<PersistentAuditEvent, UUID>');
159+
160+ jhipsterFunc . replaceContent ( this . javaDir + 'service/AuditEventService.java' , 'import java.util.Optional;' , 'import java.util.Optional;\nimport java.util.UUID;' ) ;
161+ longToUUID ( this . javaDir + 'service/AuditEventService.java' ) ;
162+ // jhipsterFunc.replaceContent(this.javaDir + 'service/AuditEventService.java', 'find(Long id)', 'find(UUID id)');
163+
164+ jhipsterFunc . replaceContent ( this . javaDir + 'service/UserService.java' , 'getUserWithAuthorities(Long id)' , 'getUserWithAuthorities(UUID id)' ) ;
165+
166+ jhipsterFunc . replaceContent ( this . javaDir + 'web/rest/AuditResource.java' , 'import java.util.List;' , 'import java.util.List;\nimport java.util.UUID;' ) ;
167+ jhipsterFunc . replaceContent ( this . javaDir + 'web/rest/AuditResource.java' , 'get(@PathVariable Long id)' , 'get(@PathVariable UUID id)' ) ;
168+
169+ jhipsterFunc . replaceContent ( this . javaDir + 'service/mapper/UserMapper.java' , 'import java.util.List;' , 'import java.util.List;\nimport java.util.UUID;' ) ;
170+ jhipsterFunc . replaceContent ( this . javaDir + 'service/mapper/UserMapper.java' , 'userFromId(Long id)' , 'userFromId(UUID id)' ) ;
171+
172+ jhipsterFunc . replaceContent ( this . javaDir + 'web/rest/vm/ManagedUserVM.java' , 'import java.util.Set;' , 'import java.util.Set;\nimport java.util.UUID;' ) ;
173+ longToUUID ( this . javaDir + 'web/rest/vm/ManagedUserVM.java' ) ;
174+
175+ jhipsterFunc . replaceContent ( this . javaDir + 'service/dto/UserDTO.java' , 'import java.util.Set;' , 'import java.util.Set;\nimport java.util.UUID;' ) ;
176+ longToUUID ( this . javaDir + 'service/dto/UserDTO.java' ) ;
58177
59- this . message = this . props . message ;
178+ jhipsterFunc . replaceContent ( this . javaDir + 'service/mapper/UserMapper.java' , 'import java.util.Set;' , 'import java.util.Set;\nimport java.util.UUID;' ) ;
179+ longToUUID ( this . javaDir + 'service/mapper/UserMapper.java' ) ;
60180
61- this . log ( 'baseName=' + this . baseName ) ;
62- this . log ( 'packageName=' + this . packageName ) ;
63- this . log ( 'angularAppName=' + this . angularAppName ) ;
64- this . log ( 'message=' + this . message ) ;
181+ longToUUID ( this . javaDir + 'service/UserService.java' ) ;
65182
66- this . template ( 'dummy.txt' , 'dummy.txt' , this , { } ) ;
183+ var file = glob . sync ( "src/main/resources/config/liquibase/changelog/*initial_schema.xml" ) [ 0 ] ;
184+ //jhipsterFunc.replaceContent(file, 'type="bigint" autoIncrement="${autoIncrement}"', 'type="uuid"');
185+ //jhipsterFunc.replaceContent(file, 'type="bigint" autoIncrement="${autoIncrement}"', 'type="uuid"');
186+ jhipsterFunc . replaceContent ( file , 'type="bigint"' , 'type="uuid"' , true ) ;
187+ jhipsterFunc . replaceContent ( file , 'autoIncrement="\\$\\{autoIncrement\\}"' , '' , true ) ;
188+
189+ jhipsterFunc . replaceContent ( 'src/main/resources/config/liquibase/users.csv' , '1;' , '8d9b707a-ddf4-11e5-b86d-9a79f06e9478;' , true ) ;
190+ jhipsterFunc . replaceContent ( 'src/main/resources/config/liquibase/users.csv' , '2;' , '8d9b7412-ddf4-11e5-b86d-9a79f06e9478;' , true ) ;
191+ jhipsterFunc . replaceContent ( 'src/main/resources/config/liquibase/users.csv' , '3;' , '8d9b77f0-ddf4-11e5-b86d-9a79f06e9478;' , true ) ;
192+ jhipsterFunc . replaceContent ( 'src/main/resources/config/liquibase/users.csv' , '4;' , '8d9b79c6-ddf4-11e5-b86d-9a79f06e9478;' , true ) ;
193+
194+ jhipsterFunc . replaceContent ( 'src/main/resources/config/liquibase/users_authorities.csv' , '1;' , '8d9b707a-ddf4-11e5-b86d-9a79f06e9478;' , true ) ;
195+ jhipsterFunc . replaceContent ( 'src/main/resources/config/liquibase/users_authorities.csv' , '3;' , '8d9b77f0-ddf4-11e5-b86d-9a79f06e9478;' , true ) ;
196+ jhipsterFunc . replaceContent ( 'src/main/resources/config/liquibase/users_authorities.csv' , '4;' , '8d9b79c6-ddf4-11e5-b86d-9a79f06e9478;' , true ) ;
197+
198+ }
67199 } ,
68200
201+ updateEntityFiles : function ( ) {
202+ // Update existing entities to enable audit
203+ this . entitiesToUpdate = this . existingEntities ;
204+ if ( this . entitiesToUpdate && this . entitiesToUpdate . length > 0 && this . entitiesToUpdate != 'none' ) {
205+ this . log ( '\n' + chalk . bold . green ( 'I\'m Updating selected entities ' ) + chalk . bold . yellow ( this . entitiesToUpdate ) ) ;
206+ var jsonObj = null ;
207+ this . auditedEntities = [ ] ;
208+
209+ this . entitiesToUpdate . forEach ( function ( entityName ) {
210+ this . auditedEntities . push ( "\"" + entityName + "\"" )
211+ {
212+ // check if repositories are already annotated
213+ var uuidGeneratorAnnotation = '@GeneratedValue.*"UUIDGenerator"' ;
214+ var pattern = new RegExp ( uuidGeneratorAnnotation , 'g' )
215+
216+ var content = this . fs . read ( this . javaDir + 'domain/' + entityName + '.java' , 'utf8' ) ;
217+
218+ if ( ! pattern . test ( content ) ) {
219+ // We need to convert this entity
220+
221+ // JAVA
222+ convertIDtoUUID ( this . javaDir + 'domain/' + entityName + '.java' , 'import java.util.Objects;' ) ;
223+
224+ // DTO
225+ if ( fs . existsSync ( this . javaDir + 'service/dto/' + entityName + 'DTO.java' ) ) {
226+ jhipsterFunc . replaceContent ( this . javaDir + 'service/dto/' + entityName + 'DTO.java' , 'import java.util.Objects;' , 'import java.util.Objects;\nimport java.util.UUID;' ) ;
227+ longToUUID ( this . javaDir + 'service/dto/' + entityName + 'DTO.java' ) ;
228+ //jhipsterFunc.replaceContent(this.javaDir + 'web/rest/dto/' + entityName + 'DTO.java', 'public Long getId()', 'public UUID getId()');
229+ //jhipsterFunc.replaceContent(this.javaDir + 'web/rest/dto/' + entityName + 'DTO.java', 'public void setId(Long id)', 'public void setId(UUID id)');
230+ }
231+
232+ // Mapper
233+ if ( fs . existsSync ( this . javaDir + 'service/mapper/' + entityName + 'Mapper.java' ) ) {
234+ jhipsterFunc . replaceContent ( this . javaDir + 'service/mapper/' + entityName + 'Mapper.java' , 'import java.util.List;' , 'import java.util.List;\nimport java.util.UUID;' ) ;
235+ longToUUID ( this . javaDir + 'service/mapper/' + entityName + 'Mapper.java' ) ;
236+ }
237+
238+ // And the Repository
239+ jhipsterFunc . replaceContent ( this . javaDir + 'repository/' + entityName + 'Repository.java' , 'import org.springframework.data.jpa.repository.*;' , 'import java.util.UUID;\nimport org.springframework.data.jpa.repository.*;' ) ;
240+ longToUUID ( this . javaDir + 'repository/' + entityName + 'Repository.java' ) ;
241+ //jhipsterFunc.replaceContent(this.javaDir + 'repository/' + entityName + 'Repository.java', 'JpaRepository<' + entityName + ',Long>', 'JpaRepository<' + entityName + ',UUID>');
242+ //jhipsterFunc.replaceContent(this.javaDir + 'repository/' + entityName + 'Repository.java', '@Param("id") Long id', '@Param("id") UUID id');
243+
244+ // The Search Repository
245+ if ( fs . existsSync ( this . javaDir + 'repository/search/' + entityName + 'SearchRepository.java' ) ) {
246+ jhipsterFunc . replaceContent ( this . javaDir + 'repository/search/' + entityName + 'SearchRepository.java' , 'import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;' , 'import java.util.UUID;\nimport org.springframework.data.elasticsearch.repository.ElasticsearchRepository;' ) ;
247+ longToUUID ( this . javaDir + 'repository/search/' + entityName + 'SearchRepository.java' ) ;
248+ //jhipsterFunc.replaceContent(this.javaDir + 'repository/search/' + entityName + 'SearchRepository.java', 'ElasticsearchRepository<' + entityName + ', Long>', 'ElasticsearchRepository<' + entityName + ',UUID>');
249+ }
250+
251+ // Service
252+ if ( fs . existsSync ( this . javaDir + 'service/' + entityName + 'Service.java' ) ) {
253+ jhipsterFunc . replaceContent ( this . javaDir + 'service/' + entityName + 'Service.java' , 'import org.springframework.data.domain.Page;' , 'import java.util.UUID;\nimport org.springframework.data.domain.Page;' ) ;
254+ longToUUID ( this . javaDir + 'service/' + entityName + 'Service.java' ) ;
255+ //jhipsterFunc.replaceContent(this.javaDir + 'service/' + entityName + 'Service.java', 'findOne(Long id)', 'findOne(UUID id)');
256+ //jhipsterFunc.replaceContent(this.javaDir + 'service/' + entityName + 'Service.java', 'delete(Long id)', 'delete(UUID id)');
257+ }
258+
259+ // ServiceImp
260+ if ( fs . existsSync ( this . javaDir + 'service/impl/' + entityName + 'ServiceImpl.java' ) ) {
261+ jhipsterFunc . replaceContent ( this . javaDir + 'service/impl/' + entityName + 'ServiceImpl.java' , 'import org.springframework.data.domain.Page;' , 'import java.util.UUID;\nimport org.springframework.data.domain.Page;' ) ;
262+ longToUUID ( this . javaDir + 'service/impl/' + entityName + 'ServiceImpl.java' ) ;
263+ //jhipsterFunc.replaceContent(this.javaDir + 'service/impl/' + entityName + 'ServiceImpl.java', 'findOne(Long id)', 'findOne(UUID id)');
264+ //jhipsterFunc.replaceContent(this.javaDir + 'service/impl/' + entityName + 'ServiceImpl.java', 'delete(Long id)', 'delete(UUID id)');
265+ }
266+
267+ // Resource
268+ jhipsterFunc . replaceContent ( this . javaDir + 'web/rest/' + entityName + 'Resource.java' , 'import java.util.List;' , 'import java.util.UUID;\nimport java.util.List;' ) ;
269+ longToUUID ( this . javaDir + 'web/rest/' + entityName + 'Resource.java' ) ;
270+ //jhipsterFunc.replaceContent(this.javaDir + 'web/rest/' + entityName + 'Resource.java', '@PathVariable Long id', '@PathVariable UUID id', true);
271+ //jhipsterFunc.replaceContent(this.javaDir + 'web/rest/' + entityName + 'Resource.java', '@PathVariable Long id', '@PathVariable UUID id');
272+
273+ // JavaScript
274+ var entityNameSpinalCased = _s . dasherize ( _s . decapitalize ( entityName ) ) ;
275+ var stateFile = glob . sync ( this . webappDir + '../webapp/app/entities/' + entityNameSpinalCased + '/' + entityNameSpinalCased + '*.state.js' ) [ 0 ] ;
276+ jhipsterFunc . replaceContent ( stateFile , '\{id\:int\}' , '{id:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}}' , true ) ;
277+
278+ // Liquidbase
279+ var file = glob . sync ( "src/main/resources/config/liquibase/changelog/*entity_" + entityName + ".xml" ) [ 0 ] ;
280+ //jhipsterFunc.replaceContent(file, 'column name="id" type="bigint" autoIncrement="\$\{autoIncrement\}"', 'column name="id" type="uuid"');
281+ jhipsterFunc . replaceContent ( file , 'type="bigint"' , 'type="uuid"' , true ) ;
282+ jhipsterFunc . replaceContent ( file , 'autoIncrement="\\$\\{autoIncrement\\}"' , '' , true ) ;
283+
284+ // Test
285+ // TODO: Fix this
286+ jhipsterFunc . replaceContent ( 'src/test/java/' + this . packageFolder + '/web/rest/' + entityName + 'ResourceIntTest.java' , 'import java.util.List;' , 'import java.util.List;\nimport java.util.UUID;' ) ;
287+ jhipsterFunc . replaceContent ( 'src/test/java/' + this . packageFolder + '/web/rest/' + entityName + 'ResourceIntTest.java' , 'getId\\(\\)\\.intValue\\(\\)' , 'getId().toString()' , true ) ;
288+ jhipsterFunc . replaceContent ( 'src/test/java/' + this . packageFolder + '/web/rest/' + entityName + 'ResourceIntTest.java' , 'Long.MAX_VALUE' , 'UUID.randomUUID()' , true ) ;
289+ //jhipsterFunc.replaceContent('src/test/java/'+this.packageFolder+'/web/rest/' + entityName + 'ResourceIntTest.java', 'getId()\.intValue()', 'getId()');
290+ //jhipsterFunc.replaceContent('src/test/java/'+this.packageFolder+'/web/rest/' + entityName + 'ResourceIntTest.java', 'getId()\.intValue()', 'getId()');
291+ }
292+ }
293+ } , this ) ;
294+ }
295+ } ,
296+
297+
69298 registering : function ( ) {
70299 try {
71300 jhipsterFunc . registerModule ( "generator-jhipster-postgresuuid-converter" , "entity" , "post" , "app" , "Postgresql Long to UUID converter" ) ;
@@ -80,6 +309,6 @@ module.exports = yeoman.Base.extend({
80309 } ,
81310
82311 end : function ( ) {
83- this . log ( 'End of postgresuuid- converter generator' ) ;
312+ this . log ( chalk . bold . green ( 'Finished running of Postgres Long Primary Keys to UUID converter. Enjoy !!!' ) ) ;
84313 }
85314} ) ;
0 commit comments