Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to access MongoItemWriter.template from a subclass [BATCH-2567] #1037

Closed
spring-projects-issues opened this issue Jan 9, 2017 · 2 comments

Comments

@spring-projects-issues
Copy link
Collaborator

Takaaki Iida opened BATCH-2567 and commented

I want to make a subclass from MongoItemWriter to override doWrite() method but my subclass can't access MongoItemWriter.template.
If my subclass has MongoOperations template variable then afterPropertiesSet() method should be overridden to skip assertion logic.

Current sample subclass code

@Setter
public class CustomMongoItemWriter<T> extends MongoItemWriter<T> {
    private MongoOperations template;

    @Override
    protected void doWrite(List<? extends T> items) {
        for (Object object : items) {
            Movie movie = (Movie) object
            Query query = Query.query(Criteria.where("_id").is(movie.getId()));
            Update update = new Update()
                    .set("tags", movie.getTags())
                    .set("update", LocalDateTime.now());
            
            template.updateFirst(query, update, Movie.class);
        }
    }
    
    @Override
    public void afterPropertiesSet() throws Exception {
        // nothing to do
        // Assert.state(template != null, "A MongoOperations implementation is required.");
    }
}

Expected sample subclass code

public class CustomMongoItemWriter<T> extends MongoItemWriter<T> {
    @Override
    protected void doWrite(List<? extends T> items) {
        for (Object object : items) {
            Movie movie = (Movie) object;
            Query query = Query.query(Criteria.where("_id").is(movie.getId()));
            Update update = new Update()
                    .set("tags", movie.getTags())
                    .set("update", LocalDateTime.now());
            
            template.updateFirst(query, update, Movie.class);
        }
    }
}

Affects: 4.0.0

Referenced from: commits cae2cab

@spring-projects-issues
Copy link
Collaborator Author

Takaaki Iida commented

PR: #447

@spring-projects-issues
Copy link
Collaborator Author

Michael Minella commented

PR merged as cae2cab.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant